我目前正在开发一个网站,其中需要用户才能上传图片文件。我有HTML表单设置,它似乎工作,这不是问题的一部分。问题是PHP部分,我获取文件上传,收集该文件的扩展名并将文件重命名为特定时间戳,然后将其上传到特定文件夹。
我有一个if
语句来检查上传,但是每次运行代码时它都会返回“error”语句,当它“echo”时,文件名就会丢失扩展名。
我试图通过语法错误检查程序运行代码并且没有任何返回,并且在运行代码时它不显示任何警报。
我不完全确定代码出错的地方。
下面是HTML表单
<form action="uploadProdu.php" method="POST" id="productUploadForm">
<input type="file" name="productImage" id="uploadImageFile" class="center-block"><br>
<input type="submit" value="Submit Product" id="submitFormButton">
</form>
下面是PHP部分
<?php
//This retrieves all the other information from the form
$productImage=($_FILES['productImage']['name']);
//Get the extension of the image uploaded
$imageExtens = pathinfo($productImage, PATHINFO_EXTENSION);
//Generate a timestamp to use and rename the file upload
$ran = round(microtime(true)) ;
//Add a dot to the random number, to then add extension later
$ran2 = $ran.".";
//Subdirectory for image upload (the targeted folder)
$target = "productimages/";
//Combine the folder, the timestamp name and the extension
$target = $target . $ran2 . $imageExtens;
if(move_uploaded_file($_FILES['productImage']['tmp_name'], $target)) {
echo "The file has been uploaded as ".$ran2.$imageExtens;
} else {
echo "Sorry, there was a problem uploading your file.";
echo "The file you tried to upload was called ".$ran2.$imageExtens;
echo "And with directory full length ".$target;
}
?>