我正在尝试使用php将文件上传到与上传脚本不在同一目录中的目录,这是dir的结构:
var>www>html>
foo>index.php //this ones where upload is done
bar> //this is where I want to upload it
index.php
文件的form
元素:
<form enctype="multipart/form-data" method="POST" action="index.php">
<fieldset style='width:33%;'>
<legend>UPLOAD PROJECT FILES</legend>
<select name="dirList" id="dirList" onChange="getSubDir(this.value)" required>
<option value="">SELECT PROJECT</option>
<?php
$path = "../";
$scan = scandir($path);
foreach($scan as $result){
if($result === '.' or $result === '..') continue;
if(is_dir($path . '/' . $result)){
echo "<option value='../$result'>$result</option>";
}
}
?>
</select>
<select name="subDirList" id="subDirList">
<option value="">SELECT PATH</option>
</select>
<input type="file" name="filePath"/>
<input type="submit" name = "upload" value="UPLOAD"/>
</fieldset>
</form>
所以这里的想法是从第一个combobox
获取文件夹列表,这将获得第二个combobox
中的子目录列表。
然后在index.php
头部的php函数上传文件:
if(isset($_POST['upload'])){
$upload_path = $_POST['subDirList'];
if($upload_path == ''){
$upload_path = $_POST['dirList'];
}
$upload_path = getcwd().'/'.$upload_path;
$upload_path = $upload_path.'/'.basename($_FILES['filePath']['name']);
if(move_uploaded_file($_FILES["filePath"]["tmp_name"], $upload_path)){
echo "success";
} else {
echo print_r($_FILES);
echo 'fail';
}
}
错误日志:
[Wed Sep 23 14:10:46.841250 2015] [:error] [pid 4028] [client 78.167.168.253:52438] PHP Warning: move_uploaded_file(): Unable to move '/tmp/phpEB1a3s' to '/var/www/html/eShop/Capture.PNG' in /var/www/html/ftp/index.php on line 16, referer: http://xxx.xx.xx.xx/ftp/index.php
答案 0 :(得分:0)
Check if you have writte permissions
Then, by documentation还检查文件名是否有效,并记住警告:
Warning If the destination file already exists, it will be overwritten.
作为建议,请务必检查:
isset($_FILES["FileInput"]) && $_FILES["FileInput"]["error"]== UPLOAD_ERR_OK)
在继续之前是真的。