PHP - 上传脚本中的神秘错误

标签 php linux upload

我正在尝试通过他们的 API 将文件上传到 vshare.io。他们提供了一个 php 脚本来执行此操作:

<?php
if(!function_exists('curl_init')) {
    die('CURL functions are not available. Debian: apt-get install php5-curl');
}

$file_path = ''; // Example: $file_path = '/home/files/file.exe';
$token = ''; // You can get your TOKEN from the following page  http://vshare.io/api.html
$post = array(
    'token'    => $token,
    'filesize' => filesize($file_path),
    'Filedata' => '@' . $file_path
);
// init
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://upload.vshare.io/upload_api.php');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:  "));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$result = curl_exec($ch);
curl_close($ch);
$output = json_decode($result, TRUE);
if(isset($output['upload'], $output['video'], $output['fileid']) && strlen($output['fileid']) == 7 && $output['upload'] == 1) {
    if($output['video'] == '1') {
        $file_type = 'video';
    } elseif($output['video'] == '0') {
        $file_type = 'file';
    }
    echo 'File Type: ' . $file_type . ' | File Link: http://vshare.io/d/' . $output['fileid'];
} else {
    echo 'Error: ' . $output['msg'];
}
?> 

我在 Ubuntu 中插入了我的 token 和正确的文件路径,但是当启动脚本时它将运行大约 1 分钟,然后它会打印“错误:”(脚本的最后一行说要这样做)。没有文件上传到我的帐户

有什么提示吗?

最佳答案

可能有两个原因。首先是你的用户代理,你还没有设置它。今天大多数网站都限制了 BOT,所以你应该为你的 CURL 设置它:

curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17');

第二个原因是 CURL 进程超时,因此您什么也得不到,也没有收到任何消息。

试试这个

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0); 
curl_setopt($ch, CURLOPT_TIMEOUT, 400);

关于PHP - 上传脚本中的神秘错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44888511/

相关文章:

php 为什么没有带有 jsonUnserialize 方法的\jsonUnserialized 接口(interface)?

mysql - 用户 'root' @'localhost' 的访问被拒绝

linux - Apache2 密码保护目录但允许本地访问而无需密码提示

php - 如何优化 PHP 代码以上传 SQL 转储

java - 使用 HTTP 连接从 Java 上传文件

javascript - 如何使用php和jquery在谷歌地图中动态添加纬度和经度?

php - 使用 UNION ALL 时出现问题

php - 匹配字符串中的单词

c++ - 在 Linux 上为可执行文件创建导入库

java - Android - 上传图像和请求数据