简体   繁体   English

如何从PC(本地主机)上载图像文件到PHP中位于服务器(例如godaddy.com)上的目录?

[英]how to upload an image file from a PC (localhost) to a directory located on server (eg. godaddy.com) in PHP?

i want to upload an image file from my client PC (ie localhost) to a directory located on remote server (ie godaddy.com). 我想将图像文件从客户端PC(即localhost)上传到位于远程服务器(即godaddy.com)上的目录。 How can i do that using PHP ? 我如何使用PHP做到这一点?

i have tried uploading from localhost to directory on the localhost itself. 我试图从本地主机上传到本地主机本身的目录。 it works fine there but doesn't works for remote server. 它在那里工作正常,但不适用于远程服务器。

below is html code: 下面是html代码:

<html>
<body>
    <form action="upload_file.php" method="POST" enctype="multipart/form-data">
           Browse for File to Upload : <br>
        <input name="file" type="file" id="file"><br>
        <input type="submit" id="u_button" name="u_button" value="Upload the file">
    </form>
</body>

And below is my PHP code: 下面是我的PHP代码:

 <?php
$file_result = "";
    if($_FILES['file']['error'] > 0){
        $file_result .= "No File uploaded or invalid File ";
        $file_result .= "Error Code: ".$_FILES["file"]["error"]."<br>";
    }
    else{
        $file_result .=
        "Upload:" . $_FILES["file"]["name"]."<br>".
        "Type:" . $_FILES["file"]["type"] . "<br>" .
        "Size:" . ($_FILES["file"]["size"]/1024) . " Kb<br>" .
        "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

        move_uploaded_file($_FILES["file"]["tmp_name"],
        "onenetwork.ddns.net/api/profile_images/" . $_FILES["file"]["name"]);

        $file_result .= "File uploaded successfully";
    }
    ?>

Below is the error i get: 以下是我得到的错误:

 Warning: move_uploaded_file(onenetwork.ddns.net/api/profile_images/hala- 
 madrid-wallpaper-hd-wallpapers.jpg): failed to open stream: No such file 
 or directory in C:\xampp\htdocs\upload_file.php on line 16

 Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\phpBFF0.tmp' 
 to 'onenetwork.ddns.net/api/profile_images/hala-madrid-wallpaper-hd- 
 wallpapers.jpg' in C:\xampp\htdocs\upload_file.php on line 16

i expect out to be file successfully uploaded.please help me out. 我希望文件能够成功上传。请帮助我。

  1. You can't move the file to onenetwork.ddns.net/api/profile_images/hala-madrid-wallpaper-hd-wallpapers.jpg . 您无法将文件移动到onenetwork.ddns.net/api/profile_images/hala-madrid-wallpaper-hd-wallpapers.jpg That is a URL, you need to specify a proper directory PATH on the server. 那是一个URL,您需要在服务器上指定一个正确的目录PATH Usually it would be something like 通常情况是

dirname(__FILE__).'/api/profile_images/'.$_FILES["file"]["tmp_name"]

  1. You cant just run the script on your local xampp install and expect it to be able to upload to a server on the internet. 您不能只在本地xampp安装上运行脚本,并希望它能够上载到Internet上的服务器。 You need to upload your upload_file.php script to the server behind onenetwork.dns.net and run it from http://onenetwork.dns.net/upload_file.php 您需要将upload_file.php脚本上传到onenetwork.dns.net后面的服务器,并从http://onenetwork.dns.net/upload_file.php运行它

  2. There are a lot of security concerns with doing this - you need to block people from being able to upload the wrong type of file (check the MIME type of the file) and you need to make sure they can't upload to directories that you dont want them to. 这样做有很多安全方面的顾虑-您需要阻止人们上传错误类型的文件(检查文件的MIME类型),并且需要确保他们无法上传到您所使用的目录不想他们。 To use this on an internet server, you will have to spend some time making it secure first unless you want to be hacked. 要在互联网服务器上使用此功能,除非您希望被黑客入侵,否则您将不得不花一些时间使其首先变得安全。

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

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