Got a form to add a product to a web store. You can upload up to 10 images, the minimum is 1 image. Even though it inserts the name of the file to the MySQL database perfectly, the files won’t move to the right folder.
I’m using a shared web host, PHP 5.2, the file has write permissions. 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 ^
2
Answers
This is definitely either a permissions problem or a path problem. Destination directory must exist and be writable by your PHP process. 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) . ‘/../assets’
Assuming all of the above is correct then it will work for you!
So what cherrysoft said is correct; 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. Here is a better alternative to your current code:
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.