简体   繁体   English

php:无法上传文件到服务器

[英]php : unable to upload file to the server

hey I am not much of a PHP coder嘿,我不是 PHP 编码器

I am using following to upload file to server acn any body help me whats wrong with this code我正在使用以下将文件上传到服务器 acn 任何机构帮助我此代码有什么问题

<?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提前感谢

I don't see anything wrong with the PHP code, though without an error it is difficult to tell what is happening.我认为 PHP 代码没有任何问题,但如果没有错误,很难判断发生了什么。

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:确保您在表单标签中有enctype="multipart/form-data

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

  2. Make sure PHP is accepting the input, by adjusting the following PHP ini variables:通过调整以下 PHP ini 变量,确保 PHP 接受输入:

    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 http://us.php.net/manual/en/ini.core.php#ini.post-max-size http://us.php.net/manual/en/info.configuration.php#ini.max -输入时间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.最后,确保为临时上传文件夹( http://us.php.net/manual/en/ini.core.php#ini.upload-tmp-dir )和您正在移动文件的文件夹正确设置了权限至。 If it is a Windows server you might also run into an inheritance issue which will require you to change the default upload directory.如果它是 Windows 服务器,您可能还会遇到 inheritance 问题,这将要求您更改默认上传目录。

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
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++;
          }



}
?>

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.你能公布你得到的错误吗?你的代码看起来没问题。这里的上传文件夹必须留在你运行代码的目录的上层。然后它应该可以工作。如果你的脚本文件夹像这样 /test/script /abc.php 那么你的上传目录应该是 /test/uploads。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM