上传后重命名文件

时间:2015-02-17 11:19:29

标签: php

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

我从这里尝试过这个答案(How to rename uploaded file before saving it into a directory?)但是出现了很多错误并且文件没有上传。

这是我的上传代码:

<?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\

1 个答案:

答案 0 :(得分:1)

<?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时间戳(以毫秒为单位)作为上传的名称,并且几乎不可能让上传覆盖另一个。 (如果你只使用兰德可能就是这种情况)。 我可能会考虑使用数据库密钥作为名称的一部分。