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 
                    $val= move_uploaded_file($_FILES['web_image']['tmp_name'], $dest);

                    chmod($dest, 0644);//make sure you have permissions for the file
                    //update database and rename
                    $filename = $_FILES['web_image']['tmp_name'];                   

Hoping someone can help.

Recommended Answers

All 3 Replies

Hi Sorry the following should have been removed -


we will give an unique name


the new name will be containing the full path where will be stored (images folder)

Member Avatar

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);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.