Home PHP & MySQL: Replace Image if the File Name (not file extension) is Identical?
Reply: 0

PHP & MySQL: Replace Image if the File Name (not file extension) is Identical?

user1529
1#
user1529 Published in June 19, 2018, 2:23 pm

For example if I uploaded "profile1.jpg" to my website, then later uploaded "profile1.png" or "profile1.gif", the new image should replace the old image if the file name is identical even if the file extension is different. My code to upload the profile image is this:

if (isset($_POST['submit'])) {
    $file = $_FILES['file'];

    $fileName = $_FILES['file']['name'];
    $fileTmpName = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];
    $fileType = $_FILES['file']['type'];
    $fileError = $_FILES['file']['error'];

    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));

    $allowed = array('jpg', 'jpeg', 'png', 'pdf');

    if (in_array($fileActualExt, $allowed)) {
        if ($fileError === 0) {
            if ($fileSize < 1000000) {
                $fileNameNew = "profile".$id.".".$fileActualExt;
                $fileDestination = 'uploads/'.$fileNameNew;
                move_uploaded_file($fileTmpName, $fileDestination); /*Something like then_replace_all_files_with_this_filename (example) */
                $sql = "UPDATE profileimg SET status=0 WHERE userid='$id';";
                $result = mysqli_query($conn, $sql);
                header("Location:index.php?upload=success");
            } else {
                echo "File too large!";
            }
        } else {
            echo "Error uploading your file!";
        }
    } else {
        echo "Format not allowed!";
    }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.456464 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO