TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-25-2008, 02:33 AM   #1 (permalink)
The Wanderer
 
Join Date: Feb 2008
Posts: 13
Thanks: 3
gillweb is on a distinguished road
Unhappy Host Upgraded - File Upload won't work now

I have had my site working for MANY years until recently my host had upgraded the server to PHP 5. Sicne then I can't get a simple file upload to work? Can anyone look at this code and tell me why it wouldn't work?

The error I am getting is "Sorry. You did not chose an image" which I am assuming means the file string isn't being passed on??? But the weirder thing is I am getting the "Ad uploaded" message also? So it's telling me there is an error but also saying it was uploaded (which it isn't).

PHP Code:
<?php
$adtitle 
$_POST['adtitle'];
$adtype $_POST['adtype'];
$imagetype $_POST['imagetype'];
$adimagefile $_POST['adimagefile'];
$url $_POST['url'];
$groupid $_POST['groupid'];


   
$error false;
   
$errormessage "";

   if (
strlen($adtitle) < 1) {
      
$error true;
      
$errormessage .= "<li><b>Sorry.  You did not enter an ad title.</b><br>\n";
   }
   if (
$adtype == 'A'){
      if (
strlen($code) < 1) {
         
$error true;
         
$errormessage .= "<li><b>Sorry.  You did not enter an affiliate code.</b><br>\n";
      }

   }

   if (
$adtype == 'O'){
      if (
strlen($adimagefile) < 1) {
         
$error true;
// HERE"S MY ERROR!!!!!
         
$errormessage .= "<li><b>Sorry.  You did not chose an image</b><br>\n";
      }
      if (
$imagetype == "") {
         
$error true;
         
$errormessage .= "<li><b>Sorry.  You did not chose an image type.</b><br>\n";
      }
      if (
strlen($url) < 1) {
         
$error true;
         
$errormessage .= "<li><b>Sorry.  You did enter a URL</b><br>\n";
      }

   }

?>

<h2>Save new ad</h2>
<p>

    <?php
$adid 
nextid("ads""adid");

        if(
$adtype == 'O'){
// HERE's MY UPLOAD
            
$newfilename "../ads/$adid.$imagetype";
            
$picfile $HTTP_POST_FILES['userfile']['tmp_name'];
            
copy($adimagefile$newfilename);
            
printf("Ad uploaded<br>");
        }

        if (
$error) {
           
printf("<font color=red>There was a problem creating the ad!</font><br><br>Please click on back in your browser to fix the problems listed below.<br>");
           
printf($errormessage);
        } else {

        if (
$adtype == 'A'){
           
$sql "insert into ads (adid, code, impressions, clicks, groupid, type, name) values ($adid, '$code', 1, 1, $groupid, '$adtype', '$adtitle')";
        }

        if (
$adtype == 'O'){
           
$newurl "http://";
           
$newurl .= $url;
           
$sql "insert into ads (adid, image, url, impressions, clicks, groupid, type, name) values ($adid, '$imagetype', '$newurl', 1, 1, $groupid, '$adtype', '$adtitle')";
        }

           
$result mysql_query($sql ,$db);

           
printf("<p>The ad has been added to the group.</p>");
           
printf("<p><a href=editadgroup.php?groupid=$groupid>Back to ad group</a></p>");

        }
    
?>

</p>
gillweb is offline  
Reply With Quote
Old 02-25-2008, 02:47 AM   #2 (permalink)
The Contributor
 
abiko's Avatar
 
Join Date: Feb 2008
Location: Croatia
Posts: 90
Thanks: 4
abiko is on a distinguished road
Default

Try this code:

PHP Code:
if($adtype == 'O'){
// HERE's MY UPLOAD
            
$newfilename "../ads/$adid.$imagetype";
            
$picfile $HTTP_POST_FILES['userfile']['tmp_name'];
    if ( 
move_uploaded_file$picfile$newfilename) ) {
            
printf("Ad uploaded<br>");
    } else {
            
printf"Ad not uploaded");
   }


Also:

PHP Code:
   if (!empty($adimagefile)) 
or
PHP Code:
      if (isset($adimagefile)) { 
see how this works for you.
Did you try
PHP Code:
print_r$_POST ); 
to see is the $_POST variables are all there?
Can you tell us how does it work. is this the upload-add script or?
__________________
Back from sysadmins to the programmers.
Send a message via ICQ to abiko Send a message via MSN to abiko
abiko is offline  
Reply With Quote
Old 02-25-2008, 02:56 AM   #3 (permalink)
The Wanderer
 
Join Date: Feb 2008
Posts: 13
Thanks: 3
gillweb is on a distinguished road
Default

this is a custom CMS backend to add advetisement banners to my website but of course now it's not working. My assumption is that PHP5 uses move_file_uploaded now instead of copy?

Ok. still didn't work. Again saying i didn't select an image? i added a var_dump(@POST); to my script and the image file is not being passed through by the script? here's my form that is submiting the info is there a problem with this maybe?

HTML Code:
<form enctype="multipart/form-data" action=savead.php method=post>
<p>
<input type=hidden name=adtype value="O">
<input type=hidden name=groupid value="2">

Title<br><input type=text name=adtitle size=50>
<br>
Upload Ad<br><input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<input name="adimagefile" type="file">
<br>

image type<br><input type="radio" name="imagetype" value="jpg"> - Jpeg &nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="imagetype" value="gif">Gif<br>
URL<br>
http://<input type=text name=url size=50>
<br>
<input type=reset value=Reset>
&nbsp;&nbsp;&nbsp;&nbsp;
<input type=submit value=Submit>
</p>
</form>
here's my $_POST variables...
PHP Code:
Array
(
    [
adtype] => O
    
[groupid] => 2
    
[adtitle] => Test Title
    
[MAX_FILE_SIZE] => 100000
    
[imagetype] => jpg
    
[url] => www.google.com

No $adimagefile
gillweb is offline  
Reply With Quote
Old 02-25-2008, 03:20 AM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Uploaded items get stored in $_FILES, not $_POST.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 02-25-2008, 03:20 AM   #5 (permalink)
The Contributor
 
abiko's Avatar
 
Join Date: Feb 2008
Location: Croatia
Posts: 90
Thanks: 4
abiko is on a distinguished road
Default

Edit: hehe Wildhoney - same post time :)
-----
oh.
YOu don't get 'adimagefile' in the $_POST -
there should on be adtitle, imagetype and url.

The adimagefile data, e.g. the data about the uploaded image is in $_FILES['adimagefile'].
Here's an array

PHP Code:
$file        =        array(                'tmp_name'    =>    $_FILES['adimagefile']['name'],
                                                                    
'tmp_type'    =>    $_FILES['adimagefile']['type'],
                                                                    
'tmp_file'    =>    $_FILES['adimagefile']['tmp_name'],
                                                                    
'tmp_size'    =>    $_FILES['adimagefile']['size']
                                                                ); 
And there you have :
  • tmp_name => the name of the file - (image1.jpg/image1.gif)
  • tmp_type => the type - image/jpeg, image/gif, image/png
  • tmpl_file => the path to the tmp file created by php in the tmp folder :)
  • tmp_size => the size of the image

Hope this works for you:
PHP Code:
<?php
$adtitle 
$_POST['adtitle'];
$adtype $_POST['adtype'];
$imagetype $_POST['imagetype'];
$url $_POST['url'];
$groupid $_POST['groupid'];
$file        =        array(                'tmp_name'    =>    $_FILES['adimagefile']['name'],
                                                                    
'tmp_type'    =>    $_FILES['adimagefile']['type'],
                                                                    
'tmp_file'    =>    $_FILES['adimagefile']['tmp_name'],
                                                                    
'tmp_size'    =>    $_FILES['adimagefile']['size']
                                                                );

   
$error false;
   
$errormessage "";

   if (
strlen($adtitle) < 1) {
      
$error true;
      
$errormessage .= "<li><b>Sorry.  You did not enter an ad title.</b><br>\n";
   }
     if (
strlen($url) < 1) {
         
$error true;
         
$errormessage .= "<li><b>Sorry.  You did enter a URL</b><br>\n";
      }

   }
    if (
strlen($code) < 1) {
                 
$error true;
                 
$errormessage .= "<li><b>Sorry.  You did not enter an affiliate code.</b><br>\n";
              }
              
              
if ( !
$error ) {    
    echo 
"<h2>Save new ad</h2>
<p>"
;
        if ( 
$file['tmp_type'] == 'image/jpeg' $file['tmp_type'] == 'image/gif') {
                            
// Upload
                              
$newfilename "../ads/$adid.$imagetype";
                    
$picfile $file['tmp_file'];
                    if ( 
move_uploaded_file$picfile$newfilename) ) {
                                
printf("Ad uploaded<br>");
                                
// Add to the DB                                  
                                           
switch ( $adtype) {
                                                case 
'A':
                                                 
$sql "insert into ads (adid, code, impressions, clicks, groupid, type, name) values ($adid, '$code', 1, 1, $groupid, '$adtype', '$adtitle')";
                                                break;    
                                                
                                                
// Right ad - upload and add to the DB
                                                
case 'O':
                                                     
$newurl "http://";
                                           
$newurl .= $url;
                                           
$sql "insert into ads (adid, image, url, impressions, clicks, groupid, type, name) values ($adid, '$imagetype', '$newurl', 1, 1, $groupid, '$adtype', '$adtitle')";                                                    
                                                break;
                                          }                                          
                                             
$result mysql_query($sql ,$db);
                                             
                                             if ( 
$result ) {
                                           
printf("<p>The ad has been added to the group.</p>");
                                           
printf("<p><a href=editadgroup.php?groupid=$groupid>Back to ad group</a></p>");
                                       } else {
                                           
printf"Error inserting into the DB");
                                       }
                            } else {
            
printf"Ad not uploaded");
                               } 
  echo 
"</p>";    
    } else {
                
printf "<li><b>Sorry.  You did not chose an image</b><br>\n";

          
} else {
      
printf("<font color=red>There was a problem creating the ad!</font><br><br>Please click on back in your browser to fix the problems listed below.<br>");
           
printf($errormessage);
    
}
__________________
Back from sysadmins to the programmers.
Send a message via ICQ to abiko Send a message via MSN to abiko
abiko is offline  
Reply With Quote
The Following User Says Thank You to abiko For This Useful Post:
gillweb (02-25-2008)
Old 02-25-2008, 03:23 AM   #6 (permalink)
The Wanderer
 
Join Date: Feb 2008
Posts: 13
Thanks: 3
gillweb is on a distinguished road
Default

should this be taken out then?
PHP Code:
$adimagefile $_POST['adimagefile']; 
gillweb is offline  
Reply With Quote
Old 02-25-2008, 03:30 AM   #7 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

It should be like the following:

php Code:
$adimagefile = $_FILES['adimagefile'];

Then you'll have like:

php Code:
$adimagefile['tmp_name'];
$adimagefile['name'];
$adimagefile['size'];

Edit: Hehe, abiko!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
gillweb (02-25-2008)
Old 02-25-2008, 03:53 AM   #8 (permalink)
The Wanderer
 
Join Date: Feb 2008
Posts: 13
Thanks: 3
gillweb is on a distinguished road
Default

thanks! Got it working.
gillweb is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 02:15 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design