1

hey I am not much of a PHP coder

I am using following to upload file to server acn any body help me whats wrong with this code

<?php 

$uploaddir = './uploads/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo "http://iphone.zcentric.com/uploads/{$file}";
}

?>

Thanx in advance

4
  • 2
    Would you tell us what happens when you run that code? Is the ./uploads/ directory existing and writable? Does $_FILES['userfile'] exist when the code is run? Is there any PHP error being reported? PS: I don't think basename() is necessary. Commented May 4, 2011 at 14:06
  • yes I am getting a PHP errors Commented May 4, 2011 at 14:24
  • Paste them in your question, all of them exactly as they appear. And verify ./uploads/ directory is writable by writing a text file with PHP into that directory. I bet we can solve this if you collaborate with us! :) Commented May 4, 2011 at 14:25
  • It's useless asking for more info while we don't see the errors. Commented May 4, 2011 at 14:35

4 Answers 4

2

I don't see anything wrong with the PHP code, though without an error it is difficult to tell what is happening.

Somethings which could cause uploads not to work, and which may not return errors:

  1. Ensure you have enctype="multipart/form-data in the form tag:

    <form enctype="multipart/form-data" action="__URL__" method="POST">

  2. Make sure PHP is accepting the input, by adjusting the following PHP ini variables:

    http://us.php.net/manual/en/ini.core.php#ini.post-max-size http://us.php.net/manual/en/info.configuration.php#ini.max-input-time http://us.php.net/manual/en/ini.core.php#ini.upload-max-filesize

  3. Finally, ensure that permissions are properly set for both the temp upload folder (http://us.php.net/manual/en/ini.core.php#ini.upload-tmp-dir) and the folder you are moving files to. If it is a Windows server you might also run into an inheritance issue which will require you to change the default upload directory.

Sign up to request clarification or add additional context in comments.

Comments

1
iF YOU WANT TO UPLOAD .pdf FILE TO LOCAL SERVER THEN USE THIS SIMPLE METHOD, Lets we are doing code here under Button Click Event...


if (isset($_POST['submit']))  
{

if ( ($_FILES["file"]["type"] =="application/pdf"))
{ 

if (file_exists("C:/xampplite/htdocs/site/upload/" . $_FILES["file"]["name"]))

    echo " This File is already exists in folder";
 else
{
  move_uploaded_file ($_FILES["file"]["tmp_name"],"C:/xampplite/htdocs/site/upload/" . $_FILES["file"]["name"]);      
  echo "File have been Stored in:-C:/xampplite/htdocs/site/upload/ "  . $_FILES["file"]["name"];

  }
}

}//end of click_event

1 Comment

this is an incredible, your solution is very handy !
0

could u pls publish what the error u get?Your code looks ok.Here the upload folder must he stay in the upper of the directory where you run the code.Then it should to work.if your script folder like this /test/script/abc.php then your uploads directory should be /test/uploads.

Comments

0
index.php
<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload[]" id="fileToUpload" multiple="">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>
upload.php
<?php
//$target_dir = "uploads/";
/*$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
*/


if(count($_FILES['fileToUpload']['name']) > 0)
{

         $i=0;
         while($i<count($_FILES['fileToUpload']['name'])) 
         {
          $filen = $_FILES["fileToUpload"]['name']["$i"]; 
          $path = 'uploads/'.$filen;
          $imageFileType = pathinfo($path,PATHINFO_EXTENSION);
          if (file_exists($path)) {
           echo "Sorry, file already exists.";
           }else  if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
           && $imageFileType != "gif" ) {
           echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";

          }

               else if(move_uploaded_file($_FILES["fileToUpload"]['tmp_name']["$i"],$path)) 
               {
               //echo "The file ". basename( $_FILES["fileToUpload"]["name"]["$i"]). " has been uploaded.";
               $files=$_FILES["fileToUpload"]["name"]["$i"];
               echo $files;?><img src="<?php echo $path;?>" style="width:200px;height:200px" alt="" >
               <?php
               } 
               $i++;
          }



}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.