0

Hi Everyone, I have the following script and I would like to rename the uploaded file to web-image(ext)

The file is being uploaded to the correct location on the server and the database is being updated correctly, But I am unable to figure out how to rename the uploaded file with the correct ext of .jpg or .png

                $img1 = $_FILES['web_image']['name'];
                //let's add a security check, to make sure only allowed files are uploaded
                $ext=explode('.',$img1);//explode the image name into an array to get the extension
                $allowed_exts=array('jpg','jpeg','png');// allowed extensions, add or remove any extension you want allowed(LOWER CASE ONLY)
                if(in_array(strtolower($ext[1]), $allowed_exts)){//file type is allowed!

                    // store image 
                    $dest="product-imgs/$productid/tmp_".$img1;
                    $val= move_uploaded_file($_FILES['web_image']['tmp_name'], $dest);

                    chmod($dest, 0644);//make sure you have permissions for the file
                    if($val)
                    {
                    //update database and rename
                    $filename = $_FILES['web_image']['tmp_name'];                   
                    imageResizes($dest,'product-imgs/'.$productid.'/web_image.jpg',strtolower($ext[1]));    

Hoping someone can help.
thanks

3
Contributors
3
Replies
34
Views
2 Years
Discussion Span
Last Post by diafol
0

Hi Sorry the following should have been removed -

 imageResizes($dest,'product-imgs/'.$productid.'/web_image.jpg',strtolower($ext[1]));  
0

we will give an unique name

$filename=time().'.'.$extension;

the new name will be containing the full path where will be stored (images folder)
$newname="images/".$image_name;

2

Never use explode to get extension. Use pathinfo()

$ext = pathinfo($_FILES['web_image']['name'], PATHINFO_EXTENSION);
$dest =  "images/uploads/" . $username . "_" . time() . "." . $ext;
$val= move_uploaded_file($_FILES['web_image']['tmp_name'], $dest);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.