PHP 8.5.0 Alpha 1 available for testing

Voting

: seven minus three?
(Example: nine)

The Note You're Voting On

chelidze dot givia at gmail dot com
2 years ago
When using move_uploaded_file(). If the user uploads an image with a name that already exists, move_uploaded_file() will overwrite it. It's a good practice to store images in directories that you generate upon creating ur card/user/product etc...

<?php
function generateDir(int $n): string {
$characters="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$dir = "";
for(
$i = 0; $i<$n; $i++){
$index = rand(0, strlen($characters)-1);
$dir .= $characters[$index];
}
return
$dir;
}

$image = $_FILES["image"];
$imagePath = 'images/'. generateDir(10) .'/'. $image["name"];

// Make the directory first or else it will not proceed with the upload.
mkdir($imagePath);

// some error handling etc...

move_uploaded_file($image["tmp_name"], $imagePath);
?>

<< Back to user notes page

To Top