-1

I'm new with PHP and im looking for a way to rename a file (images) after uploaded.

I have tried this answer from here (How to rename uploaded file before saving it into a directory?) but alot of errors came up and the file didn't uploaded.

Here's my upload code:

<?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);
}
?>

Please tell me how can i rename it to a random name.

thank you :)

Error / Notices:

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\
Community
  • 1
  • 1
yaqoob
  • 1,052
  • 3
  • 14
  • 39

1 Answers1

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);
}
?>

This will use unix timestamp (with milliseconds) as name for your upload, and make it virtually impossible to have an upload overwrite a different one. (Which may be the case if you just use rand). I would probably consider using a database key as part of the name as well.

lshas
  • 1,691
  • 1
  • 19
  • 39