简体   繁体   中英

Upload is not producing any results

I have made this script for uploading remote files but the file is not uploaded to server and the database for var1 remains empty. Here is my code.

HTML:

<form enctype="multipart/form-data" id="form1" method="post" action="">
    <p><label>Upload songs</label>      
    <input type="text" name="song"><input type="submit" value="Submit" class="button"></p>
</form>

PHP:

$uri = copy1_file($_POST['song']);
$story['v1'] = $uri;

        $url = $_POST['song'];
    $file = fopen ($url, "rb");
    if ($file) {
        $newf = fopen ($uri, "wb");
        if ($newf) {
            while(!feof($file)) {
                fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
            }
        }
    }
    if ($file) {
        fclose($file);
    }
    if ($newf) {
        fclose($newf);
    }
    else {
        return false;
    }
}

I have checked the error log but nothing is coming. Where I am doing a mistake?

Read up on PHP File Uploads .

Try this to debug your script:

var_dump($_FILES);

Do you see anything?

EDIT: Okay, I see you more clearly defined "remote files." You have to use a "text" input to receive the URL. How did you get around the standard file selection dialog?

It looks like your code will work as-is if you just change the <input/> type to "text".

EDIT 2: I noticed you updated your post to have <input type="text"/> so you already have a URL making it through to your script. Check to see if your host has allow_url_fopen enabled. If not, your URL will not be retrieved by fopen() . You will have to use cURL instead.

EDIT 3: Could it be that your copy1_file function never actually returns the uri? There are a lot of errors in the code in that function. Ex: copy1_file is receiving a url, why does it have $file['name'] at the top? And later reference $_POST directly? There are more errors. Check it closely and correct them, then it should work fine since you say fopen(url) is known to work with your host.

you need to use $_FILES array to access uploaded files instead of $_POST

here is the sample code for handling file uploads:

$pathToSaveFile = '...'; // this is the file path where the uploaded file should be saved
move_uploaded_file($_FILES['song']['tmp_name'], $pathToSaveFile);

UPDATE

if you need just to upload a file by URL then you don't need <input type="file"> , just use simple <input type="text"> and after user inserts URL and submits, you can use file_get_contents and file_put_contents functions:

file_put_contents($pathToSavePath, file_get_contents($_POST['song']));

to be able to use these functions to download remote files, make sure that allow_url_fopen option of PHP is set to TRUE

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