简体   繁体   English

上传未产生任何结果

[英]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. 我已经制作了用于上传远程文件的脚本,但是该文件未上传到服务器,并且var1的数据库仍然为空。 Here is my code. 这是我的代码。

HTML: 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: 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 . 阅读有关PHP文件上传的信息

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. 您必须使用“文本”输入来接收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". 如果仅将<input/>类型更改为“文本”,则您的代码看起来将按原样工作。

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. 编辑2:我注意到您更新了您的帖子,使其具有<input type="text"/>因此您已经有一个URL可以直达您的脚本。 Check to see if your host has allow_url_fopen enabled. 检查主机是否已启用allow_url_fopen If not, your URL will not be retrieved by fopen() . 否则, fopen()将不会检索您的URL。 You will have to use cURL instead. 您将不得不使用cURL代替。

EDIT 3: Could it be that your copy1_file function never actually returns the uri? 编辑3:可能是您的copy1_file函数从不实际返回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? 例如: copy1_file正在接收URL,为什么它的顶部有$file['name'] And later reference $_POST directly? 以后直接引用$_POST吗? 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. 仔细检查并更正它们,然后应该可以正常工作,因为您说fopen(url)已知可以与您的主机一起使用。

you need to use $_FILES array to access uploaded files instead of $_POST 您需要使用$_FILES数组访问上载的文件,而不是$_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 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: 如果只需要通过URL上传文件,则不需要<input type="file"> ,只需使用简单的<input type="text">并且在用户插入URL并提交后,可以使用file_get_contentsfile_put_contents职能:

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 为了能够使用这些功能下载远程文件,请确保将PHP的allow_url_fopen选项设置为TRUE。

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

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