[英]PHP move_uploaded_file() function not working
獲得了將產品添加到網上商店的表單。 您最多可以上傳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 ^
這肯定是權限問題或路徑問題。 目標目錄必須存在,並且可由PHP進程寫入。 對於目標,請使用絕對路徑,而不要使用相對路徑,如果要使用相對路徑,請確保使用dirname( FILE )。 '/../資產'
假設以上所有內容都是正確的,那么它將對您有用!
所以Cherrysoft所說的是正確的。 它幾乎可以肯定是權限問題,或者不符合您的想法 。
我將利用這個機會來幫助您改進代碼。 您的示例可以簡化,以便更輕松地添加10個以上的圖像,並且更易於維護。 這是當前代碼的更好替代方法:
if(isset($_POST['name'])){
foreach($_FILES as $key => $file) {
move_uploaded_file($file['tmp_name'],"../assets/img/carousel/{$file['name']}");
}
}
這樣,您的代碼就不會那么重復了。 有一些方法可以進一步改善這一點,但是我不想迷失方向。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.