John Davidson

php - Create new directory in tmp folder and store uploaded file

0 comments
Message:


I want to store a file uploaded by the user in my Temp folder (C:\Temp) using php but not sure how to go about it.



My Answer:
You can use the `mkdir()` function in PHP to create a new directory in the tmp folder and then move the uploaded file to that directory. Here's an example code snippet to achieve this:

php
$uploadDir = 'C:/Temp/';
$uploadFile = $uploadDir . basename($_FILES['file']['name']);

if (!file_exists($uploadDir)) {
mkdir($uploadDir, 0777, true);
}

if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}


In this code snippet:
1. We specify the upload directory as `C:/Temp/`.
2. We check if the directory exists, and if not, we create it using `mkdir()` with permissions `0777`.
3. We move the uploaded file from the temporary location to the specified upload directory using `move_uploaded_file()`.

Make sure to handle file validation and security measures to prevent any potential attacks.

Rate this post

3 of 5 based on 9999 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us