自动上传本地文件-Javascript PHP

问题描述 投票:0回答:1

我有一个长长的excel工作表,其中包含数百行,我需要使用其表单输入将其上传到文件中。

我已将excel转换为MySql,并在本地主机中创建了一个php脚本来执行发布。由于我无法设置文件输入的值,因此我使脚本要求将pdf和图像拖到页面上,然后表单将自动提交。这已经为我节省了很多时间,因为我不需要复制粘贴excel中的12列。

问题:我仍然希望自动上传文件,而不必拖动它们。

我尝试使用JavaScript设置文件输入的值,但确实在输入字段中看到了文件名;但文件上传为空。

<input type="file" name="image_file" id="image_file">
<script type="text/javascript">
const dT = new ClipboardEvent('').clipboardData || new DataTransfer();
dT.items.add(new File(["1"], "jpgs/001.jpg", { type: "image/jpg"}));
image_file.files = dT.files;
</script>

我想补充一点,我所有的图像和pdf均已编号(001.jpg,002.jpg ....和001.pdf,002.pdf),文件名已存储在数据库中的每一行中。

我已经看到了使用Selenium或AutoIt的建议,但是我什么都无法使用。我希望使用exec()解决方案,以防止上载的文件为空。

编辑:我尝试使用curl没有成功

$ch = curl_init();
$data = array('name' => 'imgg', 'file' => $_SERVER['DOCUMENT_ROOT'].'/images/001.jpg');
curl_setopt($ch, CURLOPT_URL, 'http://externalsite.com/post.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
javascript php selenium exec autoit
1个回答
0
投票

“从PHP 5.5.0开始,不赞成使用@前缀,并且可以使用CURLFile发送文件。”用PHP手册编写。下面是在较新的php版本中使用curl上传文件的方法。

function makeCurlFile($file){
$file = realpath($file);
$mime = mime_content_type($file);
$info = pathinfo($file);
$name = $info['basename'];
$output = new CURLFile($file, $mime, $name);
return $output;
}

$ch = curl_init("http://path/to/url/post.php");
$img =makeCurlFile('001.jpg');
$pdf =makeCurlFile('1.pdf');
$data = array('img' => $img,'pdf' => $pdf);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
if (curl_errno($ch)) {
   $result = curl_error($ch);
}
curl_close ($ch);
© www.soinside.com 2019 - 2024. All rights reserved.