简体   繁体   English

为什么我的PHP文件上传代码不起作用?

[英]Why is my PHP file upload code not working?

I am trying to make a simple file upload form using PHP. 我正在尝试使用PHP制作一个简单的文件上传表单。 Here's my code: 这是我的代码:

<?php
    $uploads_dir = '/uploads';

    if(isset($_FILES['thefile'])){
      $errors= array();
      $file_name = $_FILES['thefile']['name'];
      $file_size =$_FILES['thefile']['size'];
      $file_tmp =$_FILES['thefile']['tmp_name'];
      $file_type=$_FILES['thefile']['type'];
      $tmp_name = $_FILES['thefile']["tmp_name"];

      if($file_size > 2097152){
         $errors[]='File size must be less than 2 MB';
      }
      if(empty($errors)==true){
         move_uploaded_file($tmp_name, "$uploads_dir/$file_name");
         echo "Success";
      }
      else{
         print_r($errors);
      }
   }
?>
<!DOCTYPE html>
<html>
<head>
    <title>Simple File Upload</title>
</head>
<body>

<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="thefile" />
    <input type="submit"/>
</form>

</body>
</html>

I realize that I'm not limiting file types, but I'll worry about that once I can get a simple .jpg or .zip file uploaded. 我意识到我并没有限制文件类型,但是一旦我可以上传一个简单的.jpg或.zip文件,我就会担心。 Using the above code, I go to the page on my local server located at 使用上面的代码,我转到位于以下位置的本地服务器上的页面:

C:\wamp\www\simpleupload  (this contains index.php, the file posted above)

When I select a small image file and click submit, I'm presented with the following errors: 当我选择一个小的图像文件并单击提交时,出现以下错误:

Warning: move_uploaded_file(/uploads/session_timeout_formatting_bug.png): failed to open stream: No such file or directory in C:\\wamp\\www\\project_fileshare\\index.php on line 18 警告:move_uploaded_file(/uploads/session_timeout_formatting_bug.png):无法打开流:第18行的C:\\ wamp \\ www \\ project_fileshare \\ index.php中没有此类文件或目录

and

Warning: move_uploaded_file(): Unable to move 'C:\\wamp\\tmp\\phpEFDE.tmp' to '/uploads/session_timeout_formatting_bug.png' in C:\\wamp\\www\\project_fileshare\\index.php on line 18 警告:move_uploaded_file():无法在第18行的C:\\ wamp \\ www \\ project_fileshare \\ index.php中将'C:\\ wamp \\ tmp \\ phpEFDE.tmp'移动到'/uploads/session_timeout_formatting_bug.png'

Line 18 is the line that calls the move_uploaded_file() function. 第18行是调用move_uploaded_file()函数的行。

How do I fix this error? 如何解决此错误? I have an 'uploads_dir' folder located in the same folder as my index.php file. 我有一个'uploads_dir'文件夹,位于与index.php文件相同的文件夹中。 (reference the file path above). (请参考上面的文件路径)。 What am I doing wrong here? 我在这里做错了什么? I must be misunderstanding some small part of this process and have put my directory in the wrong place, or I'm doing something wrong in the code. 我一定会误解此过程的一小部分,并将目录放在错误的位置,否则我在代码中做错了什么。

Can someone spot my mistake and tell me what I need to do to fix it? 有人可以发现我的错误并告诉我该怎么做才能解决它?

You are working on windows and you've told PHP a location that is inaccessible (ie /uploads linux path). 您正在Windows上工作,并且已经告诉PHP无法访问的位置(即/uploads linux路径)。

Since you are working in windows and your document root is C:\\wamp\\www\\simpleupload 由于您在Windows中工作,并且文档根目录为C:\\wamp\\www\\simpleupload

Which means, your files are located like this: 也就是说,您的文件位于以下位置:

  • C:\\wamp\\www\\simpleupload\\index.php (your upload script) C:\\wamp\\www\\simpleupload\\index.php (您的上传脚本)
  • C:\\wamp\\www\\simpleupload\\uploads (where the files should be uploaded) C:\\wamp\\www\\simpleupload\\uploads (文件应上传到的位置)

Why don't you use absolute path like this: 为什么不使用这样的绝对路径:

$uploads_dir = getcwd() . DIRECTORY_SEPARATOR . 'uploads';

The getcwd() function will return the current working directory for the executing PHP script (in your case index.php), so the $uploads_dir will now look like this: C:\\wamp\\www\\simpleupload\\uploads getcwd()函数将返回正在执行的PHP脚本的当前工作目录(在您的情况下为index.php),因此$uploads_dir如下所示: C:\\wamp\\www\\simpleupload\\uploads

Try this. 尝试这个。

If your upload directory is in the same location as your index.php remove the "/" in your $uploads_dir variable. 如果您的上载目录与index.php位于同一位置,请删除$ uploads_dir变量中的“ /”。 This, or add a "." 这,或添加一个“。” before the slash because now it refers to the root which might be something else then your current working directory. 在斜线之前,因为现在它指的是根,而根可能是当前工作目录之外的其他名称。 Speaking of the devil; 说到魔鬼; http://php.net/manual/en/function.getcwd.php http://php.net/manual/zh/function.getcwd.php

$uploads_dir = getcwd() . '\uploads';
$uploads_dir = __DIR__ . '\uploads'; #php > 5.6

Also, check if your directory is writeable for php: http://php.net/manual/en/function.is-writable.php 另外,请检查您的目录是否可用于php: http : //php.net/manual/en/function.is-writable.php

Also what Latheesan said, better to go cross platform as I made the same mistake seen in my edit. 还有Latheesan所说的,最好跨平台运行,因为我犯了相同的错误。

<?php
    function buildPath(...$segments){
        return join(DIRECTORY_SEPARATOR, $segments);
    }

    echo buildPath(__DIR__, 'uploads');
?>

And i would change 我会改变

if(isset($_FILES['thefile'])){

for this 为了这

if($_FILE['thefile']['error'] === UPLOAD_ERR_OK){

Because I think that this is the best way to know if the user upload a file or the input is blank. 因为我认为这是了解用户是否上传文件或输入为空的最佳方法。

Or 要么

if (isset($_FILE["thefile"]["name"])){

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

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