简体   繁体   中英

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). How can i do that using 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>
<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
$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 . That is a URL, you need to specify a proper directory PATH on the server. 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. 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

  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. To use this on an internet server, you will have to spend some time making it secure first unless you want to be hacked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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