Jump to content

File upload problems.


yungbloodreborn

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 1 month later...

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.