Im trying to upload files to a users folder but Im getting stuck, any help? <?ph
ID: 3538353 • Letter: I
Question
Im trying to upload files to a users folder but Im getting stuck, any help?
<?php
$username=$_SESSION['username'];
$dir= 'userFolders/'.$username.'/';
if(isset($_FILES['file'])){
$image_name=$_FILES['file']['name'];
$image_size=$_FILES['file']['size'];
$image_temp=$_FILES['file']['tmp_name'];
$allowed_ext=array('jpg', 'jpeg', 'png', 'gif', 'docx', 'pdf');
$image_ext=strtolower(end(explode('.', $image_name)));
$errors=array();
//upload the image
move_uploaded_file($image_temp, $dir.$image_file);
header('Location: honey.php');
exit();
}
Explanation / Answer
The "upload_file.php" file contains the code for uploading a file:
By using the global PHP $_FILES array you can upload files from a client computer to the remote server.
The first parameter is the form's input name and the second index can be either "name", "type", "size", "tmp_name" or "error". Like this:
This is a very simple way of uploading files. For security reasons, you should add restrictions on what the user is allowed to upload.
In this script we add some restrictions to the file upload. The user may upload .gif, .jpeg, and .png files; and the file size must be under 20 kB:
The examples above create a temporary copy of the uploaded files in the PHP temp folder on the server.
The temporary copied files disappears when the script ends. To store the uploaded file we need to copy it to a different location:
The script above checks if the file already exists, if it does not, it copies the file to a folder called "upload".