0

我目前正在一个站点上工作,该站点需要用户可以上传图像文件。我有 HTML 表单设置,它似乎正在工作,这不是问题的一部分。问题是 PHP 部分,我正在上传文件,收集该文件的扩展名并将文件重命名为特定时间戳,然后将其上传到特定文件夹中。

我有一个if检查上传的语句,但是每次我运行代码时,它都会返回“错误”语句,当它“回显”文件名时,它缺少扩展名。

我试图通过语法错误检查器运行代码并且没有任何返回,并且在运行代码时它不显示任何警报。

我不完全确定代码哪里出错了。

下面是 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;
     }
     ?> 
4

0 回答 0