note 128697 added to function.move-uploaded-file

From: Date: Thu, 06 Jul 2023 14:54:33 +0000
Subject: note 128697 added to function.move-uploaded-file
Groups: php.notes 
Request: Send a blank email to php-notes+get-220658@lists.php.net to get a copy of this message
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); ?> ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.159.168 (proxied: 188.169.77.79) ---- Manual Page -- https://php.net/manual/en/function.move-uploaded-file.php Edit -- https://main.php.net/note/edit/128697 Del: integrated -- https://main.php.net/note/delete/128697/integrated Del: useless -- https://main.php.net/note/delete/128697/useless Del: bad code -- https://main.php.net/note/delete/128697/bad+code Del: spam -- https://main.php.net/note/delete/128697/spam Del: non-english -- https://main.php.net/note/delete/128697/non-english Del: in docs -- https://main.php.net/note/delete/128697/in+docs Del: other reasons-- https://main.php.net/note/delete/128697 Reject -- https://main.php.net/note/reject/128697 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220658) next »