I am new to PHP and I need some help in the below code. The below code is working fine but I need to add a "Functionality to be added in the below code, so that if any New File is uploaded it should auto increment in a series. For Example: 00004001, 00004002, 00004003.... and so on.

Please help...! Below is the existing code. Currently it Uploads the exact file name to the remote server.

<?php
if ( empty( $_FILES['file'] ) ) {
    return;
}
$ftp_server = "xx.xx.x.xxxx";
$ftp_user_name = "xxxxxxx";
$ftp_user_pass = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
$destination_file = "";
$source_file = $_FILES['file']['tmp_name'];

// set up basic connection
$conn_id = ftp_connect($ftp_server);


// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

//ftp_pasv($conn_id, true); 

// check connection
if ((!$conn_id) || (!$login_result)) { 
    echo "FTP connection has failed!";
    echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
    exit; 
}   else {


    echo "<p align=center> Your Image has been Uploaded if you dont't see any Error Msg Below</p> ";
}

// upload the file

$destination_folder = "/";
$destination_file = $destination_folder . "/" . basename($_FILES['file']['name']);
//ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 

// check upload status
if (!$upload) { 


echo "<p align=center> **File Upload Has Failed &#128558</p> ";

} else {
echo "";
}

// close the FTP stream 
ftp_close($conn_id);
?>

Dani AI

Generated

Two practical paths: use timestamp-based names (fast, collision-resistant) as suggested, or produce true sequential numbers by inspecting the remote folder, as outlined. If you must have strictly sequential eight-digit names like 00004001, the most reliable FTP-only approach is: 1) list the remote directory to find the current max number, 2) upload the file to a temporary unique name, then 3) attempt a server-side rename to the computed next name in a short retry loop. That minimizes race windows and preserves atomicity of the final move on most servers.

Example helper and usage (adjust $destination_folder and $width):

function getNextSeqNumber($conn, $dir, $width = 8) {
    $list = ftp_nlist($conn, $dir);
    if ($list === false) return 1;
    $max = 0;
    foreach ($list as $path) {
        $base = pathinfo(basename($path), PATHINFO_FILENAME);
        if (preg_match('/^\d+$/', $base)) {
            $n = (int)$base;
            if ($n > $max) $max = $n;
        }
    }
    return $max + 1;
}

$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
$width = 8;
$temp = '.tmp_' . uniqid();
ftp_put($conn, "$destination_folder/$temp", $_FILES['file']['tmp_name'], FTP_BINARY);

$tries = 0;
do {
    $num = getNextSeqNumber($conn, $destination_folder, $width);
    $final = sprintf("%0{$width}d", $num) . ($ext ? ".{$ext}" : "");
    $ok = @ftp_rename($conn, "$destination_folder/$temp", "$destination_folder/$final");
    $tries++;
} while (!$ok && $tries < 6);

Notes and cautions: test behavior on your FTP server—some servers overwrite on rename. If you expect concurrent uploads frequently, the safest solution is a small server-side API or script that issues the next sequence under an exclusive lock (flock or DB-backed). Also enable passive mode (ftp_pasv), handle ftp_nlist returning false, preserve file extensions/case, and add proper error logging.

Recommended Answers

All 4 Replies

Please Help....!!!

I had a similar problem but I didn't want to keep track of a number like yours which would have me setup/use SQL to hold/update that number. My solution was to use the date and time for part of the filename. This way I had information I needed about the file in the filename and it was easy to get a listing of files for a day or days. If I had numbers, well, more work and not exactly what we needed.
Why did we go this way? The files were from an automatic tester so with a simple ls or dir command I could tell how many tests completed on a day. In parting, this is offered as a possible solution without any offer of writing your codes.

Hmmmm,interesting

rproffitt's solution would be ideal.

Another way could be without resorting to a database.

  1. Read the folder where the file will be saved.
  2. If the folder is empty, add 000001 to the filename, then upload the file.
  3. The folder is not empty and, assuming that previous files were saved with a number sequence, fetch the last file uploaded (via timestamp), then get the file's sequence number, add 1, use that for the new file and upload it.

Good luck!

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.