获得一个将产品添加到网上商店的表格。您最多可以上传10张图像,最小为1张图像。即使将文件名完美地插入到MySQL数据库中,文件也不会移动到正确的文件夹中。
我正在使用共享的Web主机PHP 5.2,该文件具有写权限。已经与虚拟主机进行了交谈,这不是权限问题。
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="img1"><b>Image 1</b></label>
<input required type="file" class="form-control" name="img1" placeholder="Image 1 (name of the first image, upload it as instructed)">
</div>
<div class="form-group">
<label for="img2"><b>Image 2</b></label>
<input type="file" class="form-control" name="img2" placeholder="Image 2 (name of the first image, upload it as instructed)">
</div>
<div class="form-group">
<label for="img3"><b>Image 3</b></label>
<input type="file" class="form-control" name="img3" placeholder="Image 3 (name of the first image, upload it as instructed)">
</div>
<div class="form-group">
<label for="img4"><b>Image 4</b></label>
<input type="file" class="form-control" name="img4" placeholder="Image 4 (name of the first image, upload it as instructed)">
</div>
<div class="form-group">
<label for="img5"><b>Image 5</b></label>
<input type="file" class="form-control" name="img5" placeholder="Image 5 (name of the first image, upload it as instructed)">
</div>
<div class="form-group">
<label for="img6"><b>Image 6</b></label>
<input type="file" class="form-control" name="img6" placeholder="Image 6 (name of the first image, upload it as instructed)">
</div>
<div class="form-group">
<label for="img7"><b>Image 7</b></label>
<input type="file" class="form-control" name="img7" placeholder="Image 7 (name of the first image, upload it as instructed)">
</div>
<div class="form-group">
<label for="img8"><b>Image 8</b></label>
<input type="file" class="form-control" name="img8" placeholder="Image 8 (name of the first image, upload it as instructed)">
</div>
<div class="form-group">
<label for="img9"><b>Image 9</b></label>
<input type="file" class="form-control" name="img9" placeholder="Image 9">
</div>
<div class="form-group">
<label for="img10"><b>Image 10</b></label>
<input type="file" class="form-control" name="img10" placeholder="Image 10">
</div>
表格^
的图像部分 <?php
if(isset($_POST['name'])){
$img1 = $_FILES['img1']['name'];
$img2 = $_FILES['img2']['name'];
$img3 = $_FILES['img3']['name'];
$img4 = $_FILES['img4']['name'];
$img5 = $_FILES['img5']['name'];
$img6 = $_FILES['img6']['name'];
$img7 = $_FILES['img7']['name'];
$img8 = $_FILES['img8']['name'];
$img9 = $_FILES['img9']['name'];
$img10 = $_FILES['img10']['name'];
move_uploaded_file($_FILES['img1']['tmp_name'],"../assets/img/carousel/");
move_uploaded_file($_FILES['img2']['tmp_name'],"../assets/img/carousel/$img2");
move_uploaded_file($_FILES['img3']['tmp_name'],"../assets/img/carousel/$img3");
move_uploaded_file($_FILES['img4']['tmp_name'],"../assets/img/carousel/$img4");
move_uploaded_file($_FILES['img5']['tmp_name'],"../assets/img/carousel/$img5");
move_uploaded_file($_FILES['img6']['tmp_name'],"../assets/img/carousel/$img6");
move_uploaded_file($_FILES['img7']['tmp_name'],"../assets/img/carousel/$img7");
move_uploaded_file($_FILES['img8']['tmp_name'],"../assets/img/carousel/$img8");
move_uploaded_file($_FILES['img9']['tmp_name'],"../assets/img/carousel/$img9");
move_uploaded_file($_FILES['img10']['tmp_name'],"../assets/img/carousel/$img10");
获取文件名并将其移动到正确文件夹的部分。
文件没有移到旋转木马文件夹,而是根本没有移到任何地方:
[Thu Apr 18 18:53:34.249395 2019]
[cgi:error] [pid 13994]
[client 172.69.130.13:32306] AH01215: PHP Warning: move_uploaded_file():
Unable to move '/tmp/phpOjN6YW' to '../assets/img/carousel/download (2).jpg'
in /home/seniorte/public_html/admin/add.php
on line 338: /usr/local/cpanel/cgi-sys/ea-php56
Error log ^
答案 0 :(得分:1)
这肯定是权限问题或路径问题。目标目录必须存在,并且可由PHP进程写入。对于目标,请使用绝对路径,而不要使用相对路径,如果要使用相对路径,请确保使用dirname( FILE )。 '/../ assets'
假设以上所有内容均正确,那么它将对您有用!
答案 1 :(得分:1)
所以Cherrysoft所说的是正确的;几乎可以肯定是权限问题,或者没有达到您认为的路径。
尽管如此,我将借此机会帮助您改进代码。您的示例可以简化,以便更轻松地添加10个以上的图像,并且更易于维护。这是您当前代码的更好替代方法:
if(isset($_POST['name'])){
foreach($_FILES as $key => $file) {
move_uploaded_file($file['tmp_name'],"../assets/img/carousel/{$file['name']}");
}
}
这样,您的代码就不会那么重复了。有一些方法可以进一步改善这一点,但是我不想被迷住。