Okay so i have this script that will upload and remane the file to image.png which works perfect.
what i am trying to get is when uploaded number the image in order
image1.png
image2.png
image3.png

    if (isset($_POST['submit'])) {

        $newext = '.png';
        $filename = $_FILES["file"]["name"];
        $file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
        $file_ext = substr($filename, strripos($filename, '.')); // get file name
        $filesize = $_FILES["file"]["size"];
        $allowed_file_types = array('.jpg','.png','.gif');

        if (in_array($file_ext,$allowed_file_types)  &&  ($filesize < 20000)) {

            // rename file
            $newfilename = image . $newext;

            {       
                move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newfilename);
                echo "File uploaded successfully.";         
            }

        } elseif (empty($file_basename)) {  
            // file selection error
            echo "Please select a file to upload.";     
        } elseif ($filesize > 20000) {  
            // file size error
            echo "The file you are trying to upload is too large.";     
        } else {    
            // file type error
            echo "Only these file typs are allowed for upload: " . implode(', ',$allowed_file_types);
            unlink($_FILES["file"]["tmp_name"]);        
        }

    }

thank you.

Recommended Answers

You'll need to store the latest used number somewhere, so you can retrieve it and use it when uploading the next file.

Jump to Post

All 3 Replies

You'll need to store the latest used number somewhere, so you can retrieve it and use it when uploading the next file.

Great thank you so much pritaeas.

  if (isset($_POST['submit'])) {
    $id = 1+intval(file_get_contents('next.txt'));
    $newext = '.png';
    $filename = $_FILES["file"]["name"];
    $file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
    $file_ext = substr($filename, strripos($filename, '.')); // get file name
    $filesize = $_FILES["file"]["size"];
    $allowed_file_types = array('.jpg','.png','.gif');
    if (in_array($file_ext,$allowed_file_types) && ($filesize < 20000)) {
    // rename file
    $newfilename = $id . $newext;
    {
    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newfilename);
    echo "File uploaded successfully.";
    file_put_contents("next.txt", $newfilename);
    }
    } elseif (empty($file_basename)) {
    // file selection error
    echo "Please select a file to upload.";
    } elseif ($filesize > 20000) {
    // file size error
    echo "The file you are trying to upload is too large.";
    } else {
    // file type error
    echo "Only these file typs are allowed for upload: " . implode(', ',$allowed_file_types);
    unlink($_FILES["file"]["tmp_name"]);
    }
    }

but now it just saves as
1.png
2.png
what i would like to do is
1image.png

got it all i did was

$newext = 'image.png';

thanks again pritaeas

Be a part of the DaniWeb community

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