[SOLVED] upload code doesn't upload files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

[SOLVED] upload code doesn't upload files

Post by ddragas »

Why this code doesn't upload files?
Please take a look

file form.html

Code: Select all

<body>
<form enctype="multipart/form-data" action="uploadaj.php" method="POST">
Upload file: <input name="file_name" type="file">
<input type="submit" value="Uploadaj">
</form>
</body>

file uploadaj.php

Code: Select all

<?php


$temp_filename = $_FILES['file_name']['tmp_name'];
$ime_filea_na_disku = $_FILES['file_name']['name'];
$filename = $_FILES['file_name']['name']; // get file name
$size_u_bajtovima = $_FILES['file_name']['size'];
$extension = strtolower(strrchr($filename,".")); // get everything after the last . and include the . Also lowercases the string for easier file checking
$extensions = array('.gif','.jpg','.jpeg','.png');  // Allowed File Types 
if (!in_array($extension, $extensions))
	 	{ 
	 		echo "File type not allowed." . "<br>";
			echo $extension; 
		} 
			else
				{
					$zeljena_max_velicina = 1024*500; // 500 KB
						if($size_u_bajtovima > $zeljena_max_velicina + 1) 
							{
  								die('Poslali ste prevelik file');
							}
								else
									{

										$Photo_Dir = "uplodani_fajlovi/";//"../uploadedfiles/apartmani/"; 
										$userID = "674893279/";//$Sifra_apartmana ."/"; 

	   										if (!is_dir($Photo_Dir . $userID)) 
							               { 
							                    mkdir ($Photo_Dir . $userID, 0777); 
							                    chmod ($Photo_Dir . $userID, 0777); 
							               }
										   		

												srand((double)microtime()*1000000);

												$uploadfile = $Photo_Dir . $userID . rand(5000,1000000) . $extension;
												
												if(copy($_FILES['userfile']['tmp_name'], $uploadfile)){
								
												echo $_FILES['userfile']['tmp_name'] . " Fajl je uploadan u " . $uploadfile;}

?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

What is happening as of now?
Any output? Errors?

Have you checked print_r($_FILES) to see if the file is correctly being submitted?
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post by hongco »

line 39 if(copy($_FILES['userfile']['tmp_name'], $uploadfile))

should've been:

Code: Select all

if(copy($_FILES['file_name']['tmp_name'], $uploadfile))
file_name instead of userfile.
CheetahShrk
Forum Newbie
Posts: 3
Joined: Sat Mar 12, 2005 6:11 pm

Post by CheetahShrk »

Are you sure the code just isn't cut off because the code in the code box is missing some closing...
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

thank you folks. It is working now.
Post Reply