开发者

move_uploaded_file() can't open stream

开发者 https://www.devze.com 2023-01-21 15:08 出处:网络
I\'m trying to upload files via Pycurl through POST, using a PHP script on the receiving end. For some reason the PHP part seems to cause an error. I\'ve distilled it down to two short test scripts.

I'm trying to upload files via Pycurl through POST, using a PHP script on the receiving end. For some reason the PHP part seems to cause an error. I've distilled it down to two short test scripts.

import pycurl

UPLOADURL = "http://127.0.0.1/~nevon/receive.php" #URL to the PHP script
PATH = "/home/nevon/Desktop/testfile" #path to the file I'm trying to upload

c = pycurl.Curl()
c.setopt(c.POST, 1)
c.setopt(c.URL, UPLOADURL)
c.setopt( c.HTTPPOST, [("uploaded", (c.FORM_FILE, PATH))] )
c.perform()
c.close()

receive.php:

<?php 
ini_set('display_errors','On');
$target = getcwd()."/upload/"; 
$target = $target . basename( $_FILES['uploaded']['name']) ; $ok=1; 

if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { 
    echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
    } else { 
    echo "Something went wrong."; 
    } 
?>

The errors I'm receiving when testing this are:

Warning: move_uploaded_file(/home/nevon/public_html/upload/testfile): failed to open stream: No such file or directory in /home/nevon/public_html/receive.php on line 7

Warning: move_uploaded_file(): Unable to move '/tmp/phpevKFCC' to '/home/nevon/public_html/upload/testfile' in /home/nevon/public_html/receive.php on line 7

It doesn't seem like a permissions problem, but nevertheless I've checked the permissions of both the uploading directory and /tmp. I've also checked so that it isn't a problem with open_basedir restriction.

In the PHP manual on move_uploaded_file() it says the following:

move_uploaded_file() ensures the safety of this operation by allowing only those files uploaded through PHP to be moved.

Since I'm uploading via Python with Curl, I figured that might be the problem. But isn't there supposed to be a different error message if that was the case? If that is indeed the problem, what could I do instead? I haven't really used PHP much for years now, so you could say I'm a little rusty.

I'm not sure if it's relevant, but just in case, I'm testing this on Ubuntu 10.10 with apache 2.2.16, PHP 5.3.3 and Python 2.6.6.

EDIT: The plot thickens! A person on Twitter took my PHP script and put it on his server, to see if he could find out what the problem was. It worked fine for him, when uploading via a regular PHP/HTML form. So I pointed my Python script to his PHP script, and all of a sudden it worked just fine. So now I'm wondering, what开发者_如何学编程's wrong on my end...? How do I troubleshoot this?


Your script looks in order. It is clearly a problem with the $target directory. Make 10000% sure that the directory /upload already exists. move_uploaded_file() can't create it for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消