Hello.
I can't seem to remember how to move a image file (png, jpg, gif) from one folder to another, could anyone please remind me.
So far, I've built a page/form that will allow me to upload a file, and then pass the form data to a php file. This works fine.
The php (below) works fine, but will only upload the file to a folder near the php file, not the folder I want it to. Then it renames the file to a <number>.ext .
It will upload, the image file to /upload instead of the folder /Account/profile/image.
:- I have tried changing the path to /Account/profile/image/ but it didn't work.
I'd just like to ask, how do I move the file, or would be better to use the upload folder instead?
Thanks.
pppp
I can't seem to remember how to move a image file (png, jpg, gif) from one folder to another, could anyone please remind me.
So far, I've built a page/form that will allow me to upload a file, and then pass the form data to a php file. This works fine.
The php (below) works fine, but will only upload the file to a folder near the php file, not the folder I want it to. Then it renames the file to a <number>.ext .
<?php
//Upload Image
if(session_status() !== PHP_SESSION_ACTIVE) {
session_start();
}
$user_id = $_SESSION['account_id'];
$_SESSION['account_id'] = 0; //Temporary line, and willbe removed later.
// Check if the form was submitted
//https://www.tutorialrepublic.com/php-tutorial/php-file-upload.php
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Check if file was uploaded without errors
if(isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0){
$allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
$filename = $_FILES["photo"]["name"];
$filetype = $_FILES["photo"]["type"];
$filesize = $_FILES["photo"]["size"];
// Verify file extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format.");
// Verify file size - 5MB maximum
$maxsize = 5 * 1024 * 1024;
if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
// Verify MYME type of the file
if(in_array($filetype, $allowed)){
// Check whether file exists before uploading it
if(file_exists("upload/" . $filename)){
echo $filename . " is already exists.";
} else{
move_uploaded_file($_FILES["photo"]["tmp_name"], "upload/" . $filename);
echo "Your file was uploaded successfully.<br>";
}
} else{
echo "Error: There was a problem uploading your file. Please try again.";
}
} else{
echo "Error: " . $_FILES["photo"]["error"];
}
}
$entryfile = $_SESSION['account_id'].".".$ext;
rename("upload/".$filename,"upload/".$entryfile);
?>
It will upload, the image file to /upload instead of the folder /Account/profile/image.
:- I have tried changing the path to /Account/profile/image/ but it didn't work.
I'd just like to ask, how do I move the file, or would be better to use the upload folder instead?
Thanks.
pppp