First to upload files we need to create a form:
[code]
ok first take notice of the input name uploads[], the [] symbols is telling php that it will be an array of files so when we collect the info in upload-file.php we have to treat everything as an array
so next we need to create upload-file.php:
<?php
$id=rand(1,9999999); //this will give us a random value to create a unique directory
if(!is_dir("uploads/".$id)){ //this checks to make sure the directory does not already exist
mkdir("uploads/".$id, 0777, true); //if the directory doesn't exist then make it
chmod("uploads/".$id, 0777); //chmod to 777 lets us write to the directory
}
$uploaddir='uploads/' . $id .'/'; //lets put the directory into a variable notice added slash on end
foreach($_FILES["uploads"]["name"] as $bla=> $boo){ //we have to do a loop to get all the filenames
$file=$uploaddir.$boo; //we will check the filename in the upload directory, see if it exists
if (file_exists($file)) { //if it exists then ......
die("Filename already exists, please rename this file"); //if filename exists in the directory then we die!!! :P
}
}
Now that we checked to see if it is in the directory and it wasn’t so we created the directory, now we need to take the uploaded info and save it to the directory:
foreach ($_FILES["uploads"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
echo"$error_codes[$error]"; // let you know if there was an error on any uploads
move_uploaded_file( //php function to move the file
$_FILES["uploads"]["tmp_name"][$key], //from the temporary directory
$uploaddir. $_FILES["uploads"]["name"][$key] //to the directory you chose
) or die("Problems with upload");
}
}
if there was no errors then now we have our file in our directory if you want to add a link to the file you could do something like:
foreach($_FILES["uploads"]["name"] as $bla=> $boo){
$file=$uploaddir.$boo;
echo"<a href=".$file.">Click here to see".$file."<br>";
}
this is a simple script with no validation, I would suggest that you validate everything you allow to be uploaded to your server else be hacked!
Here is a validation to make sure they are image file extensions:
$allowedExtensions = array("jpg","jpeg","gif","png");
foreach ($_FILES as $file) {
if ($file['tmp_name'] > '') {
if (!in_array(end(explode(".",
strtolower($file['name']))),
$allowedExtensions)) {
die($file['name'].' is an invalid file type!<br/>'.
'<a href="javascript:history.go(-1);">'.
'<< Go Back</a>');
}
}
}
Hope this tutorial helps somebody!!!