所以我传递了一个来自type ="文件的文件数组"输入,我想将它们发送到服务器上的文件夹。 move_uploaded_file()不起作用。
我只是希望首先上传一个文件,并在之后为整个数组制作循环或其他内容。
我确实有enctype =" multipart / form-data"在HTML格式。路径正确,该文件夹为空。
试图在内部if之前设置$ image_paths [0] = $ images [0] [" tmp_name"]并且在第一个if内部返回" / tmp / php2SytkN" 。这是tmp_name的样子吗?还试图返回$ images [0] ['尺寸']并且它是正确的,所以文件就在那里。
function upload_to_file($images){
$image_paths = array();
$target_dir = "uploads/images/";
$path = $target_dir . basename($images[0]["name"]);
if(isset($images[0]) && $images[0]['size'] > 0) {
if (move_uploaded_file($images[0]["tmp_name"], $path)) {
$image_paths[0] = $path;
}
}
return $image_paths;
}
编辑:我尝试了但我不认为$ _FILES [0]会起作用,因为我已经在使用它了。我还需要第二个$ _FILES数组来存储视频,所以我想创建自己的数组。
她试图做的更详细:
if(isset($_POST['submit'])){//main form is submitted
// upload each of the selected images into a directory on the server and return the array of paths for each one:
$image_paths = upload_to_file($_SESSION['images']);
//then insert those paths as varchar in a database table and return the id of the new table row:
$images_fk = upload_to_db($image_paths);
//finally insert the the id of the umages row into the main table along with all the other form stuff:
$query = "INSERT INTO main_table (...other stuff from the main form..., images_fk) VALUES (...other stuff from the main form...'{$images_fk}')";
$result = mysqli_query($db, $query);
}elseif(isset($_POST['submit_image'])){//image form is submitted
if(count($_SESSION['images']) < 5){
//store the file into a session array so a new image can be selected with the same input type=file:
array_push($_SESSION['images'], $_FILES['image_file']);
}
}
编辑:HTML部分:
//a form with one type=file input that submits to the top of the page:
<form id="form" action="home.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="8000000">
<input class="upload_btn" type="file" name="image_file" id="image_file">
//some mix of php and html to show the names of uploaded items as you select them:
<?php for ($i = 0; $i < count($_SESSION['images']); $i++) {
if($_SESSION['images'][$i] != null){
echo "<p>";
echo htmlentities($_SESSION['images'][$i]['name']) . "  ";
echo "<a href='image_remove.php?ind=" . urlencode($i) . "'>Remove</a></p>";
}
} ?>
<input type="submit" id="img_submit" class="form_button" name="submit_image" value="upload"/>
</form>
//Then do the same form for videos....