yungbloodreborn Posted February 4, 2008 Share Posted February 4, 2008 Using the examples I've found here, and other places on the net, I've been trying to build a scripts that will allow the admins of my site to upload pictures. I've done it almost exactly according to the examples I've seen, but nothing seems to work. I've tried both copy & move_uploaded_file, and I never get the file. Here's my code: <?php session_start(); $pagename = "Admin Picture Upload"; include '../../cgi-bin/db.php'; include '../../cgi-bin/config.php'; include '../../cgi-bin/functions.php'; include '../header.php'; echo "<h1 align=center>Admin Picture Upload</h1>"; // Check if user is an admin, if not just stop. if (!is_admin($s_user_level)) exit(); $p_enter = $_POST["enter"]; $p_user = $_POST["user"]; $p_location = $_POST["location"]; //Debugging line echo "Enter: $p_enter<br> User: $p_user<br> Location: $p_location<br>"; if (!$p_enter) { echo "<form enctype='multipart/form-data' method='post' action=''>$table width='50%' align=center> <tr><td><b>Please choose a file:</b></td><td><input name='uploaded' type='file'></td></tr> <tr><td><b>Location:</b></td><td>Profile/Emoticon</td></tr> <tr><td><b>User:</b></td><td>Kevin/Millie/etc...</td></tr> <tr><td> </td><td><input type='submit' value='Upload'></td></tr> </table><input type='hidden' name='user' value='kevinisnekkid'><input type='hidden' name='location' value='emoticon'><input type='hidden' name='enter' value='yup'></form>"; exit(); } $target = "../images/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $ok=1; if ($uploaded_size > 350000) $err_size=1; if ($uploaded_type =="text/php") $err_type=1; //Debugging lines $ip = trim($_SERVER['REMOTE_ADDR']); $cpu = gethostbyaddr($ip); echo "Target: $target<br>IP: $ip<br>Host: $cpu<br>"; echo "<pre>"; print_r($_FILES); echo "</pre>"; if (is_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) echo "Yup<br>"; else echo "Nope<br>"; //End of debugging lines. if (($err_size) || ($err_type)) { echo "Sorry your file was not uploaded:<br>"; if ($err_size) echo "File too large.<br>"; if ($err_type) echo "File type not accepted.<br>"; } else { // if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { if(!copy($_FILES['uploaded']['tmp_name'], $target)) { echo "<p align=center><b>The file ".basename( $_FILES['uploaded']['name'])." has been uploaded.</b></p>"; } else { echo "<p align=center><b>There has been an error uploading the file. Please contact the webmaster.</b></p>"; } } ?> </td> </tr> </table> </body> </html> Here's the results I get: Enter: yup User: kevin Location: emoticon Target: ../images/guitar.gif IP: 222.222.222.222 Host: 222-222-222-222.fake-host.net Array ( [uploaded] => Array ( [name] => guitar.gif [type] => image/gif [tmp_name] => /tmp/phpUKSHvz [error] => 0 [size] => 1279 ) ) Nope The file guitar.gif has been uploaded. I'm puzzled... The form displays, I select a pic that's within specs. If I use move_uploaded_file, it never works. If I use copy, it says it works, but the file never arrives in the dir. Quote Link to comment https://forums.phpfreaks.com/topic/89423-file-upload-problems/ Share on other sites More sharing options...
budimir Posted February 4, 2008 Share Posted February 4, 2008 And where is your move_uploaded_file in the script????? You are not uploading file for now! Quote Link to comment https://forums.phpfreaks.com/topic/89423-file-upload-problems/#findComment-457907 Share on other sites More sharing options...
yungbloodreborn Posted February 4, 2008 Author Share Posted February 4, 2008 It's there near the end. // if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { if(!copy($_FILES['uploaded']['tmp_name'], $target)) { echo "<p align=center><b>The file ".basename( $_FILES['uploaded']['name'])." has been uploaded.</b></p>"; } else { echo "<p align=center><b>There has been an error uploading the file. Please contact the webmaster.</b></p>"; } } Ok, when I posted the example, I had copy enabled instead of move_uploaded_file. I just swapped it, and commented out the copy line, and it still doesn't work. Here's the result: Enter: yup User: kevinisnekkid Location: emoticon Target: ../images/complove.gif IP: 69.238.82.38 Host: adsl-69-238-82-38.dsl.sntc01.pacbell.net Array ( [uploaded] => Array ( [name] => complove.gif [type] => image/gif [tmp_name] => /tmp/phptFpovd [error] => 0 [size] => 10761 ) ) Nope There has been an error uploading the file. Please contact the webmaster. Quote Link to comment https://forums.phpfreaks.com/topic/89423-file-upload-problems/#findComment-458007 Share on other sites More sharing options...
PFMaBiSmAd Posted February 4, 2008 Share Posted February 4, 2008 You need to turn on full php error reporting and check your web server log file for errors. Either the $target path does not exist or is not at the relative location to the script or there is a folder/file permission problem. Quote Link to comment https://forums.phpfreaks.com/topic/89423-file-upload-problems/#findComment-458029 Share on other sites More sharing options...
yungbloodreborn Posted February 5, 2008 Author Share Posted February 5, 2008 I have error_reporting=E_ALL in php.ini And I don't see anything in error_log relating to it. I see php errors from squirrelmail that I need to check on. But I see nothing from my script. True, I'm new to doing hosting from the kind of setup I have right now. (I'm using a virtual root account on a box from a hosting company) One piece I'm concerned about is: if (is_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) echo "Yup<br>"; else echo "Nope<br>"; It replies "Nope". php.ini does allow uploaded files. I'm confused. How long are files normally kept in /tmp from a file upload? I don't think the file is ever landing there. Quote Link to comment https://forums.phpfreaks.com/topic/89423-file-upload-problems/#findComment-458167 Share on other sites More sharing options...
PFMaBiSmAd Posted February 5, 2008 Share Posted February 5, 2008 The syntax for is_uploaded_file() is not what you have. That line is likely generating an error and is failing. Quote Link to comment https://forums.phpfreaks.com/topic/89423-file-upload-problems/#findComment-458173 Share on other sites More sharing options...
yungbloodreborn Posted March 13, 2008 Author Share Posted March 13, 2008 Thanks for the note about the is_uploaded_file. I got that fixed, and it now reports correctly. I also got the upload working. I discovered it wasn't an issue with the code, it was a problem with my directory permissions. (Duh) For those interested, here's my complete, one file upload script. If it says contact webmaster, check target path & permissions... <?php echo "<html><head><title>Picture Upload</title></head><body><h1>Picture Upload</h1>"; $p_enter = $_POST["enter"]; // Display form if it hasn't been submitted yet. if (!$p_enter) { echo "<form enctype='multipart/form-data' method='post' action=''><table align=center> <tr><td><b>Please choose a file:</b></td><td><input name='uploaded' type='file'></td></tr> <tr><td> </td><td><input type='submit' value='Upload'></td></tr> </table><input type='hidden' name='enter' value='yup'></form>"; exit(); } // This is where the files go to... $target = "../images/"; $target = $target . basename( $_FILES['uploaded']['name']) ; // Size & Type limiting... if ($uploaded_size > 350000) $err_size=1; if ($uploaded_type == "text/php") $err_type=1; // Check for errors, if everything is ok, move the file. if (($err_size) || ($err_type)) { echo "Sorry your file was not uploaded:<br>"; if ($err_size) echo "File too large.<br>"; if ($err_type) echo "File type not accepted.<br>"; } else { if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "<p align=center><b>The file ".basename( $_FILES['uploaded']['name'])." has been uploaded.</b></p>"; } else { echo "<p align=center><b>There has been an error uploading the file. Please contact the webmaster.</b></p>"; } } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/89423-file-upload-problems/#findComment-491518 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.