doujiangqu2823 2010-10-21 21:04
浏览 133
已采纳

move_uploaded_file()无法打开流

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?

  • 写回答

1条回答 默认 最新

  • dtf24224 2010-10-21 21:42
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab