简体   繁体   English

PHP move_uploaded_file()函数不起作用

[英]PHP move_uploaded_file() function not working

Got a form to add a product to a web store. 获得了将产品添加到网上商店的表单。 You can upload up to 10 images, the minimum is 1 image. 您最多可以上传10张图像,最小为1张图像。 Even though it inserts the name of the file to the MySQL database perfectly, the files won't move to the right folder. 即使将文件名完美地插入到MySQL数据库中,文件也不会移动到正确的文件夹中。

I'm using a shared web host, PHP 5.2, the file has write permissions. 我正在使用共享的Web主机PHP 5.2,该文件具有写权限。 Talked to the web host already and it's not a permissions problem. 已经与虚拟主机进行过交谈,这不是权限问题。

<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>

image part of the form ^ 表格的图像部分^

    <?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");

The part that gets the name of the files & moves them to the right folder. 获取文件名称并将其移动到正确文件夹的部分。

Instead of the files moving to the carousel folder, the files are just not moving anywhere at all: 这些文件没有移到旋转木马文件夹,而是根本没有移到任何地方:

[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 ^

This is definitely either a permissions problem or a path problem. 这肯定是权限问题或路径问题。 Destination directory must exist and be writable by your PHP process. 目标目录必须存在,并且可由PHP进程写入。 For destination, use absolute path, not relative as you are using or if you are using relative make sure that you are using dirname( FILE ) . 对于目标,请使用绝对路径,而不要使用相对路径,如果要使用相对路径,请确保使用dirname( FILE )。 '/../assets' '/../资产'

Assuming all of the above is correct then it will work for you! 假设以上所有内容都是正确的,那么它将对您有用!

So what cherrysoft said is correct; 所以Cherrysoft所说的是正确的。 its almost certainly a permissions issue, or its not going to the path you think it is going to. 它几乎可以肯定是权限问题,或者不符合您的想法

I am going to use this opportunity to help you improve your code though. 我将利用这个机会来帮助您改进代码。 Your example could be streamlined to allow the addition of more than 10 images more easily, and be more maintainable. 您的示例可以简化,以便更轻松地添加10个以上的图像,并且更易于维护。 Here is a better alternative to your current code: 这是当前代码的更好替代方法:

if(isset($_POST['name'])){
    foreach($_FILES as $key => $file) {
        move_uploaded_file($file['tmp_name'],"../assets/img/carousel/{$file['name']}");
    }
}

In this way your code is not so repetitive. 这样,您的代码就不会那么重复了。 There are ways to improve this even further, but I don't want to get carried away. 有一些方法可以进一步改善这一点,但是我不想迷失方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM