无法通过php中的curl上传文件
我有一个HTML代码,允许我在服务器上成功上传文件无法通过php中的curl上传文件,php,curl,file-upload,Php,Curl,File Upload,我有一个HTML代码,允许我在服务器上成功上传文件 <form action="http://aa.bb.ccc.dd/xxx/upload.php" method="post" enctype="multipart/form-data"> <label for="text">Campaign:</label> <input type="text" name="campaign" value="abcde" readonl
<form action="http://aa.bb.ccc.dd/xxx/upload.php" method="post" enctype="multipart/form-data">
<label for="text">Campaign:</label>
<input type="text" name="campaign" value="abcde" readonly="readonly"/><br/>
<label for="file">Upload type:</label>
<input type="text" name="filename" value="0.csv" readonly="readonly"/><br/>
<label for="file">Filename:</label>
<input type="file" name="file" id="file"/><br/>
<input type="submit" name="submit" value="Submit" />
</form>
结果也是1,但是当我签入服务器时,文件没有上传。我错过了什么?
任何帮助都将不胜感激 我对使用PHP“cURL”很感兴趣。这看起来是一个相当标准的要求 我看了一些“web上的cURL PHP示例” 事实上,使用你发布的代码,我看不出有什么不对劲 不管怎样,我使用了你的代码并创建了类似的脚本。唉,你没有发布你的“upload.php”脚本。我已经创建了一个PHP手册中提到的验证工具: 尽管此示例适用于“localhost”。我用一个“真正的”外部主机运行它,它运行得很好 使用PHP5.3.28在windows和Linux上的PHP5.3.18上进行测试 Html表单在“for”方面与标签有些混淆:
<form action="process_uploaded_file.php" method="post" enctype="multipart/form-data">
<label for="text">Campaign:</label>
<input type="text" name="campaign" value="abcde" readonly="readonly"/><br/>
<label for="filetype">Upload type:</label>
<input type="text" name="filetype" value="0.csv" readonly="readonly"/><br/>
<label for="file">Filename:</label>
<input type="file" name="file" id="file"/><br/>
<input type="submit" name="submit" value="Submit" />
</form>
活动:
上载类型:
文件名:
卷曲脚本:
<?php
$target_url = 'http://localhost/testmysql/process_uploaded_file.php';
$full_path_to_source_file = __DIR__ .'/sourcefiles/testupload1.csv' ;
$post = array('campaign' => 'abcde', 'file'=>'@'. $full_path_to_source_file, 'filename' => 'curl1.csv');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);
var_dump($result);
您可以突出显示并整理您的代码吗,它似乎有重复的代码框。可以使用CURLOPT_SAFE_UPLOAD在PHP5.5中禁用文件上载的“@”符号(尽管默认情况下是允许的)。如果您已经使用了5.5,那么最好改用它。
<?php
$target_url = 'http://localhost/testmysql/process_uploaded_file.php';
$full_path_to_source_file = __DIR__ .'/sourcefiles/testupload1.csv' ;
$post = array('campaign' => 'abcde', 'file'=>'@'. $full_path_to_source_file, 'filename' => 'curl1.csv');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);
var_dump($result);
<?php session_start();
define('BIGGEST_FILE', 256 * 1024); // max upload file size
define('UPLOAD_DIRECTORY', 'P:/developer/xampp/htdocs/uploadedfiles'); // my data upload directory
if (empty($_FILES)) { // process the uploaded file...
die('no input file provided... '. __FILE__.__LINE__);
}
/* */
// validate the data -- see http://www.php.net/manual/en/features.file-upload.php
try {
// Undefined | Multiple Files | $_FILES Corruption Attack
// If this request falls under any of them, treat it invalid.
if ( !isset($_FILES['file']['error'])
|| is_array($_FILES['file']['error'])) {
throw new RuntimeException('Invalid parameters.');
}
// Check $_FILES['file']['error'] value.
switch ($_FILES['file']['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
throw new RuntimeException('No file sent.');
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
throw new RuntimeException('Exceeded filesize limit.');
default:
throw new RuntimeException('Unknown errors.');
}
// You should also check filesize here.
if ($_FILES['file']['size'] > BIGGEST_FILE) {
throw new RuntimeException('Exceeded filesize limit.');
}
// DO NOT TRUST $_FILES['file']['mime'] VALUE !!
// Check MIME Type by yourself.
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimeType = finfo_file($finfo, $_FILES['file']['tmp_name']);
/* */
if (false === $fileExt = array_search($mimeType,
array(
'jpg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'csv' => 'text/plain',
),
true
)) {
throw new RuntimeException('Invalid file format.');
}
// check 'campaign' for safe in filename...
if (preg_match('/\w/', $_POST['campaign']) !== false) {
$campaign = $_POST['campaign'];
}
else {
$campaign = md5($_POST['campaign']); // sort of useful
}
// Now move the file to my data directory
// You should name it uniquely.
// DO NOT USE $_FILES['file']['name'] WITHOUT ANY VALIDATION !!
// On this example, obtain safe unique name from its 'campaign' and 'tmp_name'.
$destFilename = sprintf('campaign_%s_%s.%s',
$campaign,
sha1_file($_FILES['file']['tmp_name']),
$fileExt);
if (!move_uploaded_file($_FILES['file']['tmp_name'],
UPLOAD_DIRECTORY .'/'. $destFilename)) {
throw new RuntimeException('Failed to move uploaded file.');
}
echo $_FILES['file']['tmp_name'], ' uploaded to: ', UPLOAD_DIRECTORY .'/'. $destFilename;
} catch (RuntimeException $e) {
echo $e->getMessage();
}