Jump to content

[SOLVED] PHP Upload - Error Checking


grahamb314

Recommended Posts

Hi all, I have this messy code below:

 

It's been a while since I last looked at it and I need some help to:

A) Limit upload size to 5mb

B) replace this symbol: '  with nothing because the directory that is being uploaded to is created in this code and the dir can not have a ' in it since php puts a \ infront of it so the directory name messes up!

 

(The session at the top is the upload directory)

- I can also not remember which variable is the actual file to upload! - Otherwise I might be able to do this myself!

 

Hope you understand!

 

Thanks,

 

Code:

<?php
$filename = "../../../../shares/shows/{$_SESSION['directory']}";
//$filename = "/uploads/{$_SESSION['directory']}";


if (is_dir($filename)) {

echo "The folder: $filename exists";
echo "<br>";
$allowed_types = array('mp3', 'mp2', 'mp1', 'wav', 'ogg');
//mp3, mp2, mp1, wav and ogga


	foreach($_FILES as $file_name => $file_array) {


		if (is_uploaded_file($file_array["tmp_name"])) {

		$image_extension = strtolower(str_replace(' ', '', $file_array["name"]));
		$image_extension = explode('.', $image_extension);
		$image_extension = strtolower($image_extension[count($image_extension) - 1]);

			if (in_array($image_extension, $allowed_types)) {

			move_uploaded_file($file_array["tmp_name"], $filename.'/'.$file_array["name"]) or die 		("Couldn't copy");
			echo "The File: ".$file_array["name"]."<br/>\n";
			echo "Was uploaded";
			echo "TEST"; 

			} 	
			else {

				echo "Filetype not allowed. You can only upload mp3, mp2, mp1, wav and ogg files ";
			}
		}

}
}



else {
    

mkdir("{$filename}", 0700);
echo "The folder did not exist but has now been created";
echo "<br>";
$allowed_types = array('mp3', 'mp2', 'mp1', 'wav', 'ogg');
//mp3, mp2, mp1, wav and ogg


	foreach($_FILES as $file_name => $file_array) {


		if (is_uploaded_file($file_array["tmp_name"])) {

		$image_extension = strtolower(str_replace(' ', '', $file_array["name"]));
		$image_extension = explode('.', $image_extension);
		$image_extension = strtolower($image_extension[count($image_extension) - 1]);

			if (in_array($image_extension, $allowed_types)) {

			move_uploaded_file($file_array["tmp_name"], $filename.'/'.$file_array["name"]) or die 		("Couldn't copy");
			echo "The File: ".$file_array["name"]."<br/>\n";
			echo "Was uploaded";
			//echo $filename;

			} 	
			else {

				echo "Filetype not allowed. You can only upload mp3, mp2, mp1, wav and ogg files ";
			}
		}

}
}

?>

Link to comment
https://forums.phpfreaks.com/topic/129586-solved-php-upload-error-checking/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.