0

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.

2
Contributors
3
Replies
30
Views
4 Years
Discussion Span
Last Post by Tko_1
0

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

0

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

Edited by Tko_1

1

got it all i did was

$newext = 'image.png';

thanks again pritaeas

This question has already been answered. 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.