简体   繁体   English

无法使用php move_uploaded_file()移动上载的文件

[英]Cant move uploaded file using php move_uploaded_file()

I have a page which allows users to post an advert on my website. 我有一个页面,允许用户在我的网站上发布广告。 This page allows them to upload multiple photos of the product in question. 该页面允许他们上载有关产品的多张照片。

I currently have two forms. 我目前有两种形式。 The first handles details of the product, while the second is a dropzone (using dropzone.js) allowing them to upload photos. 第一个处理产品的详细信息,第二个处理一个dropzone(使用dropzone.js),允许他们上传照片。 I am keen to keep these forms split as the photos could potentially be very large and I would prefer them to upload instantly rather than waiting for the user to press submit. 我希望将这些表格分开,因为照片可能非常大,我希望它们立即上传而不是等待用户按提交。 This should save time overall. 这样总体上可以节省时间。

Therefore, when the photos are put into dropzone, they are uploaded to a tmp directory (in case the user does not post the entire advert). 因此,将照片放入dropzone后,它们会上传到tmp目录(以防用户未发布整个广告)。 Once the user submits the first form, I am using move_uploaded_file() to move the file to its permanent location. 用户提交第一个表单后,我将使用move_uploaded_file()将文件移动到其永久位置。 This is the code I am using: 这是我正在使用的代码:

for ($loop=1; $loop<=6; $loop++) {

    // Retrieve filename and path
    $file_path = "image_".$loop;
    $tmp_path = $form_data[$file_path]; // $form_data is an array of POST data and contains the file_path of the image in the tmp dir

    // Move file
    $destination_path = 'images/adverts/'.$advert->id;
    $success = move_uploaded_file($tmp_path, $destination_path);

}

However, move_uploaded_file() is not working. 但是,move_uploaded_file()无法正常工作。 After consulting the documentation, I can see that move_uploaded_file() checks to see whether the file was uploaded using HTTP POST (which it was). 在查阅文档之后,我可以看到move_uploaded_file()检查是否使用HTTP POST上传了文件(是)。 To be sure, I ran a quick test to see whether is_uploaded_file() returned TRUE on my uploaded file, but it didn't. 可以肯定的是,我进行了一项快速测试,以查看is_uploaded_file()在上传的文件上是否返回TRUE,但是没有返回。

How does is_uploaded_file() determine whether a file was uploaded or not? is_uploaded_file()如何确定文件是否已上传? I was wondering whether by the time I come to move the file, php has forgotten it has been uploaded? 我想知道当我来移动文件时,php是否忘记了它已被上传? Is there any other reason my file would not pass this? 还有其他原因导致我的文件无法通过此操作吗?

Many thanks 非常感谢

I think Ive got to the bottom of this... 我想我已经深入到此...

move_uploaded_file() only seems to work in the function that the POST data is sent to. move_uploaded_file()似乎仅在POST数据发送到的函数中起作用。 In my case, I had two separate POST requests sent. 就我而言,我发送了两个单独的POST请求。 The first from dropzone.js which stored the image in a tmp directory. 来自dropzone.js的第一个文件将图像存储在tmp目录中。 If I had run the is_uploaded_file() in the function that received that POST request, it returned TRUE. 如果我在接收该POST请求的函数中运行了is_uploaded_file(),则返回TRUE。

However, once uploaded to a tmp directory, I then inserted paths in hidden fields in the second form. 但是,一旦上传到tmp目录,我便以第二种形式在隐藏字段中插入了路径。 I was trying to run move_uploaded_file() (which in turn uses is_uploaded_file()) in the function which receives the POST data from the second form. 我试图在从第二种形式接收POST数据的函数中运行move_uploaded_file()(依次使用is_uploaded_file())。 However, as the file was not part of this POST data (just the path to the tmp dir), is_uploaded_file() then returned false. 但是,由于文件不是此POST数据的一部分(只是tmp dir的路径),因此is_uploaded_file()返回false。

I hope that helps someone... 我希望这可以帮助某人...

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

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