上传后重命名文件


Renaming File after upload

我是PHP新手,正在寻找上传后重命名文件(图像)的方法。

我在这里尝试过这个答案(如何在将上传的文件保存到目录之前重命名它?)但出现了很多错误,文件没有上传。

这是我的上传代码:

<?php
if(isset($_POST['btn_upload']))
{
    $prodname = $_POST['pname'];
    $prodec = $_POST['pdesc'];
    $prodprice = $_POST['pprice'];
    $prodffers = $_POST['poffers'];
    $filetmp = $_FILES["file_img"]["tmp_name"];
    $filename = $_FILES["file_img"]["name"];
    $filetype = $_FILES["file_img"]["type"];
    $filesize = $_FILES["file_img"]["size"];
    $fileinfo = getimagesize($_FILES["file_img"]["tmp_name"]);
    $filewidth = $fileinfo[0];
    $fileheight = $fileinfo[1];
    $filepath = "../static/products/".$filename;
    $filepath_thumb = "../static/products/thumbs/".$filename;
    move_uploaded_file($filetmp,$filepath);

if($filetype == "image/jpeg")
    {
$imagecreate = "imagecreatefromjpeg";
$imageformat = "imagejpeg";
    }
if($filetype == "image/png")
{                                                 
$imagecreate = "imagecreatefrompng";
$imageformat = "imagepng";
}
if($filetype == "image/gif")
{                                                 
$imagecreate= "imagecreatefromgif";
$imageformat = "imagegif";
}
$new_width = "400";
$new_height = "400";
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = $imagecreate($filepath); //photo folder
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $filewidth, $fileheight);
$imageformat($image_p, $filepath_thumb);//thumb folder  
$sql = "INSERT INTO statement";
$result = mysql_query($sql);
}
?>

请告诉我如何将其重命名为随机名称。

谢谢:)

错误/通知:

Notice: Undefined index: file in C:'wamp'www'
Notice: Undefined variable: newfilename in C:'wamp'www'
Warning: move_uploaded_file() expects parameter 1 to be string, array given in C:'wamp'www'
Warning: imagecreatefromjpeg(../static/products/46149.): failed to open stream: No such file or directory in C:'wamp'www'
Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in C:'wamp'www'
<?php
if(isset($_POST['btn_upload']))
{
    $prodname = $_POST['pname'];
    $prodec = $_POST['pdesc'];
    $prodprice = $_POST['pprice'];
    $prodffers = $_POST['poffers'];
    $filetmp = $_FILES["file_img"]["tmp_name"];
    $filename = $_FILES["file_img"]["name"];
    $filetype = $_FILES["file_img"]["type"];
    $filesize = $_FILES["file_img"]["size"];
    $fileinfo = getimagesize($_FILES["file_img"]["tmp_name"]);
    $filewidth = $fileinfo[0];
    $fileheight = $fileinfo[1];
    // GETS FILE EXTENSION
    $fileextension = pathinfo($filename, PATHINFO_EXTENSION);
    $microtime = microtime();
    $filepath = "../static/products/".$microtime.".".$fileextension;
    $filepath_thumb = "../static/products/thumbs/".$microtime.".".$fileextension;
    move_uploaded_file($filetmp,$filepath);

    if($filetype == "image/jpeg")
    {
        $imagecreate = "imagecreatefromjpeg";
        $imageformat = "imagejpeg";
    }
if($filetype == "image/png")
{                                                 
    $imagecreate = "imagecreatefrompng";
    $imageformat = "imagepng";
}
if($filetype == "image/gif")
{                                                 
    $imagecreate= "imagecreatefromgif";
    $imageformat = "imagegif";
}
    $new_width = "400";
    $new_height = "400";
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = $imagecreate($filepath); //photo folder
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $filewidth, $fileheight);
    $imageformat($image_p, $filepath_thumb);//thumb folder  
    $sql = "INSERT INTO statement";
    $result = mysql_query($sql);
}
?>

这将使用unix时间戳(以毫秒为单位)作为上传的名称,并使上传几乎不可能覆盖其他上传。(如果您只使用rand,则可能会出现这种情况)。我可能也会考虑使用数据库键作为名称的一部分。