在上传PHP Mysql过程中重命名图像


Renaming an image during the uploading PHP Mysql

使用以下代码,有人能向我解释如何在上传过程中将图像文件重命名为其他名称吗?

以下是我正在使用的内容。

上传器.php

<?php
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");
$dataType = mysql_real_escape_string($_POST["dataType"]);
$title = mysql_real_escape_string($_POST["title"]);
$fileName = basename($_FILES["image"]["name"]);
$target_path = ($_SERVER['DOCUMENT_ROOT'] . "/images/gallery/".$fileName);
if (file_exists($target_path))
{
    echo "An image with that file name already exists.";
}
elseif (move_uploaded_file($_FILES["image"]["tmp_name"], $target_path))
{
    // The file is in the images/gallery folder. Insert record into database by
    // executing the following query:
     $sql="INSERT INTO images (data_type, title, file_name)"."VALUES('$dataType','$title','$fileName')";
     $retval = mysql_query($sql);
    echo "The image was successfully uploaded and added to the gallery :) <a href='index.php'>Add another image</a>";

}
else
{
    echo "There was an error uploading the file, please try again!";
}
?>

下面是我将图片上传到图库的代码。


<form enctype="multipart/form-data" action="uploader.php" method="POST">

        Category: <select class="text" name="dataType">
        <option value="treeremoval" selected="selected">treeremoval</option>
        <option value="treetrimming" >treetrimming</option>
        <option value="treebracing" >treebracing</option>
        <option value="stumpgrinding" >stumpgrinding</option>
        <option value="firewood" >firewood</option>
        <option value="cleanup" >cleanup</option>
        </select><br />
<br />


    Caption: <input type="text" name="title"><br />
<br />
    Image to upload: <input type="file" name="image"><br />
<br />


    <input type="submit" value="Upload">
</form>

我是php和mysql的新手,所以如果有任何帮助,我将不胜感激。我还有一些其他问题,但我想我应该一次问一个

谢谢!

我会尝试这样的方法,您将创建一个唯一的id并将文件的扩展名附加到它,如果该名称存在,则循环直到您有一个不存在的名称,然后移动文件。

<?php
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");
$dataType = mysql_real_escape_string($_POST["dataType"]);
$title = mysql_real_escape_string($_POST["title"]);
$fileData = pathinfo(basename($_FILES["image"]["name"]));
$fileName = uniqid() . '.' . $fileData['extension'];
$target_path = ($_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $fileName);
while(file_exists($target_path))
{
    $fileName = uniqid() . '.' . $fileData['extension'];
    $target_path = ($_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $fileName);
}
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_path))
{
    // The file is in the images/gallery folder. Insert record into database by
    // executing the following query:
     $sql="INSERT INTO images (data_type, title, file_name)"."VALUES('$dataType','$title','$fileName')";
     $retval = mysql_query($sql);
    echo "The image was successfully uploaded and added to the gallery :) <a href='index.php'>Add another image</a>";

}
else
{
    echo "There was an error uploading the file, please try again!";
}

?>

您正在执行move_uploaded_file($_FILES["image"]["tmp_name"], $target_path)

您的$target_path来自变量$fileName

从更改文件名

$fileName = basename($_FILES["image"]["name"]);

'myfilename'

请尝试此代码:

$sql =mysql_query("select * from table ");
$result = mysql_num_rows($sql) +1;
$newfilename = rename("$oldfilename", "$newfilename");

我建议上传一个安全的图像格式并限制文件大小

           //image upload
            $pro_image = $_FILES['image']['name'];
            $pro_image_tmp = $_FILES['image']['tmp_name'];
            $fileType = $_FILES["image"]["type"];  
            $fileSize = $_FILES["image"]["size"];  
            $fileErrorMsg = $_FILES["image"]["error"];  
             $fileName = preg_replace('#[^a-z.0-9]#i', '', $pro_image); 
             $nwtourimg = explode(".", $fileName);
             if (!$pro_image_tmp) { // if file not chosen
                echo "ERROR: Please browse for a file before clicking the upload button.";
                exit();
            } else if($fileSize > 5242880) { // if file size is larger than 5 Megabytes
                echo "ERROR: Your file was larger than 5 Megabytes in size.";
                unlink($pro_image_tmp); // Remove the uploaded file from the PHP temp folder
                exit();
            } else if (!preg_match("/.(gif|jpg|png|jpeg)$/i", $fileName) ) {
                 // This condition is only if you wish to allow uploading of specific file types    
                 echo "ERROR: Your image was not .gif, .jpg, or .png.";
                 unlink($pro_image_tmp); // Remove the uploaded file from the PHP temp folder
                 exit();
            } else if ($fileErrorMsg == 1) { // if file upload error key is equal to 1
                echo "ERROR: An error occured while processing the file. Try again.";
                exit();
            }
             $fileExt = end($nwtourimg);
             $fileName = time().rand().".".$fileExt;

             move_uploaded_file("$pro_image_tmp","upload/$fileName");
                //image upload end

//图片上传$pro_image=$_FILES['image']['name'];$pro_image_tmp=$_FILES['image']['tmp_name'];

        $fileType = $_FILES["image"]["type"];  
        $fileSize = $_FILES["image"]["size"];  
        $fileErrorMsg = $_FILES["image"]["error"];  
         $fileName = preg_replace('#[^a-z.0-9]#i', '', $pro_image); 
         $nwtourimg = explode(".", $fileName);
         if (!$pro_image_tmp) { // if file not chosen
            echo "ERROR: Please browse for a file before clicking the upload button.";
            exit();
        } else if($fileSize > 5242880) { // if file size is larger than 5 Megabytes
            echo "ERROR: Your file was larger than 5 Megabytes in size.";
            unlink($pro_image_tmp); // Remove the uploaded file from the PHP temp folder
            exit();
        } else if (!preg_match("/.(gif|jpg|png|jpeg)$/i", $fileName) ) {
             // This condition is only if you wish to allow uploading of specific file types    
             echo "ERROR: Your image was not .gif, .jpg, or .png.";
             unlink($pro_image_tmp); // Remove the uploaded file from the PHP temp folder
             exit();
        } else if ($fileErrorMsg == 1) { // if file upload error key is equal to 1
            echo "ERROR: An error occured while processing the file. Try again.";
            exit();
        }
         $fileExt = end($nwtourimg);
         $fileName = time().rand().".".$fileExt;

         move_uploaded_file("$pro_image_tmp","upload/$fileName");
            //image upload end