Jump to content

[SOLVED] PHP File Upload Problem


idlelimey

Recommended Posts

Hi,  I have written a very simple file upload script but it fails for reasons I can't explain.  Hopefully someone here can help me out.  I have checked php.ini and file uploads are on with a limit of 10M.  Here's the code:

 

The Form

<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

 

And the PHP

$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
  {echo "File has been uploaded";} 
else
  {echo "The upload did not work.";}

 

Any help is appreciated.  Assuming the target path should be [dir where the form and php are]/uploads - this exists and is 777'd.

 

Thank you :)

Link to comment
Share on other sites

Hey this work fine at my end

 

testme.php

<form enctype="multipart/form-data" action="upload.php" method="POST">

<input type="hidden" name="MAX_FILE_SIZE" value="100000" />

Choose a file to upload: <input name="uploadedfile" type="file" /><br />

<input type="submit" value="Upload File" />

</form>

 

 

upload.php

<?php

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))

  {echo "File has been uploaded";}

else

  {echo "The upload did not work.";}

?>

 

/uploads exists in the same dir where these scripts are placed and it's 777.

 

It works smoothly @ my end I received no error.

Link to comment
Share on other sites

Thank you both.  Sorry, I should have been clearer in my original post.  I believe the code to be fine (it worked for priti, thanks :) ), there's not really a lot to go wrong with it.

 

My assumption is that there is a configuration issue at the server which is preventing me from performing the upload (I get the 'else{echo "The upload did not work.";}' bit).  I am comparing two different setups (one that works, one that doesn't) at the minute to try and narrow down the problem but I'm drawing a blank.

 

Can anyone tell me any specific settings at the server end that enable file uploads?  Or, for that matter, any common settings that would fail uploads?

 

Thanks again, I am grateful for the help.

Link to comment
Share on other sites

Have just discovered the:

 

$_FILES['uploadedfile']['error']

 

and the returned value is '6', which apparently means I am missing a temporary folder.  I will look in to this some more and report back.  Any assistance on temporary folders most welcome!  ;D

Link to comment
Share on other sites

Okay, problem solved.  I have written to the host (it's on a shared service) and requested they setup a 'upload_tmp_dir'.  That'll work.  Well worth checking for anybody setting up an uploader to check the values in php.ini that will effect it.  I am guessing that 'upload_tmp_dir' usually has a default setting of '/tmp' but the host in this case has removed it.

 

For the record, there seems no way of the user setting this on the fly using .htaccess or any other similar method.

 

Thanks for the help!

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.