【问题标题】:Getting error with move_uploaded_file HTTP wrapper does not support writeable connectionsmove_uploaded_file HTTP 包装器出现错误不支持可写连接
【发布时间】:2013-12-08 13:10:59
【问题描述】:

我目前正在使用 php 进行项目。在这里,我想使用move_uploaded_file 将图像存储到文件夹中,但是当我使用以下代码时:

if (move_uploaded_file($_FILES['file_promo_image']['tmp_name'], $uploadfile))
    {
        echo "Le fichier est valide, et a été téléchargé avec succès.\n";

    }

我收到以下错误:

Warning:  move_uploaded_file(http://www.desgensbien.com/sites/bestinfo/images/news/CodeCogsEqn.gif) [function.move-uploaded-file]: failed to open stream: HTTP wrapper does not support writeable connections. in /homez.534/desgensb/www/sites/bestinfo/admin/record-news.php on line 73



Warning:  move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpfCHv2s' to 'http://www.desgensbien.com/sites/bestinfo/images/news/CodeCogsEqn.gif' in /homez.534/desgensb/www/sites/bestinfo/admin/record-news.php on line 73

我该如何解决这个问题

【问题讨论】:

  • $uploadfile 不能是 http 地址。它必须是文件系统路径。
  • 那么我怎样才能找到文件系统路径意味着php中有什么函数可以找到文件系统路径
  • 您已经在错误消息 /homez.534/..... 中看到它
  • 当我使用这个文件路径时,我得到了错误,没有这样的文件...

标签: php


【解决方案1】:

使用这个:

$uploadfile = $_SERVER['DOCUMENT_ROOT'] . '/sites/bestinfo/images/news/CodeCogsEqn.gif';

【讨论】:

  • 优秀的答案! +1
  • 需要解释:s
【解决方案2】:

你好

我知道票是前一阵子开的,但我一直在寻找这个完整的代码,我只在网上找到了它的一部分,但没有一个完全有效的,所以在这里是。 我希望它可以帮助一些人。



我看到了您的帖子,并且使用 here 找到的大部分信息,我遇到了完全相同的问题 - w3schools 以下代码的主要作者。

这是如何做到的,有两种方法:

1-您的上传表单或函数在接收文件的服务器上

// the index.php file on the server
<br><br>
<?php /* includes */ require_once 'upload.php'; ?>

<!DOCTYPE html>
<html><body> Formulaire de téléchargement d'une image <br><br>

<form action="upload.php" method="POST" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>
</body> </html>


upload.php 文件,在本例中位于同一目录中

<?php
// writing in a sublfolder called image 
$target_dir = "image/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {

    // give a name and direction to the image - i changed the name to test.jpg...
    $uploadfile = $_SERVER['DOCUMENT_ROOT'].$target_dir.'test.jpg';
    echo "<br><br>";
    echo $uploadfile;
         echo "<br><br>";

    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $uploadfile)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

?>



2- 第二个选项,如果您的网络服务器与接收文件的服务器不同,这里您需要在每台服务器上有两个文件:

发送文件的服务器(你的网络服务器):send.php

<?php
    // I uploaded a file called sample2.jpeg from on server to another
    // again you can make functions out of this
    $target_url = 'http://192.168.56.103/receive.php';
        //This needs to be the full path to the file you want to send.


$file_name_with_full_path = realpath('./sample2.jpeg');
            /* curl will accept an array here too.
             * Many examples I found showed a url-encoded string instead.
             * Take note that the 'key' in the array will be the key that shows up in the
             * $_FILES array of the accept script
             */
        //$post = array('extra_info' => '123456','file_contents'=>'@'.$file_name_with_full_path);
        $post = array('file_contents' => new CurlFile($file_name_with_full_path, 'text/plain' /* MIME-Type */,'' /*directory*/));

            $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$target_url);
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        $result=curl_exec ($ch);
        curl_close ($ch);
        echo $result;
    ?>


和接收服务器端的文件:receive.php

<?php
// you can chage the destination path
$uploaddir = realpath('./') . '/';
$uploadfile = $uploaddir . basename($_FILES['file_contents']['name']);

echo "<br><br>";
print_r ($uploadfile);
echo "<br><br>";

echo '<pre>';
        if (move_uploaded_file($_FILES['file_contents']['tmp_name'], $uploadfile)) {
            echo "File is valid, and was successfully uploaded.\n";
        } else {
            echo "Possible file upload attack!\n";
        }
        echo 'Here is some more debugging info:';
        print_r($_FILES);
        echo "\n<hr />\n";
        print_r($_POST);
print "</pr" . "e>\n";
?>



<br><br>Sources and references to other codes : 
<br>http://stackoverflow.com/questions/12667797/using-curl-to-upload-post-data-with-files
<br>http://stackoverflow.com/questions/15200632/how-to-upload-file-using-curl-with-php
<br>http://code.stephenmorley.org/php/sending-files-using-curl/
<br>http://www.w3schools.com/php/php_file_upload.asp

再次感谢 99% 代码的真正作者,修复了人们在 stackoverflow 论坛上的评论:derakkilgo 和 W3SCHOOLS。

【讨论】:

    【解决方案3】:

    很简单……是FTP的主要精髓;跨不同域写入数据。使用 PHP FTP 功能上传文件。你可以在这里查看参考库>>FTP_FPUT()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      相关资源
      最近更新 更多