Hi!
Im pretty new to this whole thing with PHP and have looked at some examples to learn it.

I currently made a site that for now, runs on WAMP localy.

The thing is, i was wondering if this would be a good code to use on the upload.php site. I get this error when trying to upload a big image file.

It says the filename can not be empty at line 85.

I would appreciate it if anyone had time to look at this for me.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Puffys Image Uploader</title>
    <link rel="stylesheet" type="text/css" href="resources/home.css"  />
	<link rel="shortcut icon" type="image/png" href="#" />
</head>

<body bgcolor="#232323">
	<div id="noFooter">
    <div id="top" align="center">
      <div id="logo"><a href="http://www.puffys.net"><img src="resources/logo.png"  height="312" widht="502" /></a></div>
        
        <div id="navigation">
       	  <ul>
            	<li><a href="#">-Photoshop</a></li>
                <li><a href="#">--Bloag</a></li>
                <li><a href="mailto:kaizokupuffball@gmail.com">---Contact</a></li>
            </ul>
        </div>
        
	</div>
    
    <div id="content" align="center">
    		<div id="uploader">            
				<?php
	
					//Database connect
					include 'script_dbconnect_pc.php';
	
					//Defines the max file size in Kb
					define ("MAX_SIZE","4096");

					//Checks the file extensions
					function getExtension($str){
						$i = strrpos($str,".");
						if (!$i) { return ""; }
						$I = strlen($str) - $i;
						$ext = substr($str,$i+1,$I);
						return $ext;
					}
	
					//If error, value change to 1, file will not be uploaded
					$errors=0;
	
					if (isset($_POST['Submit']))
					{
						//Reads the image name
						$image=$_FILES['image']['name'];
						//If not empty
						if  ($image)
						{
							//Get orgiginal name from client machine
							$filename = stripslashes($_FILES['image']['name']);
							//Get the extension
							$extension = getExtension($filename);
							$extension = strtolower($extension);
			
							//If file extension not known, error. If not, continue
							if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
							{
								//Error message
								echo 'Only JPG, JPEG, GIF and PNG allowed!';
								$errors = 1;
							}
							else
							{
								//Get imagesize in bytes
								$size = filesize($_FILES['image']['tmp_name']);
				
								//Compare size to max allowed, error if image is bigger
								if ($size > MAX_SIZE*2048)
								{
									echo 'The image is to big to be uploaded here.';
									$errors = 1;
								}
				
								//Giving the uploaded files new name
								$image_name = time().'.'.$extension;
								//Uploaded image will have the path as a name
								$newname = "storage/funny_images/".$image_name;
								//Error when copying image to storage
								$copied = copy($_FILES['image']['tmp_name'], $newname);
				
								if (!$copied)
								{
									echo 'Could not copy the image to storage.';
									$errors = 1;
								}}}}
								
					//If no errors do this
					if (isset($_POST['Submit']) && !$errors)
					{
					echo 'File uploaded successfully!';
					$sql = "INSERT INTO images (id, name, path) VALUES ('', '$image_name', '$newname')";
								
					$result = mysql_query($sql);
					mysql_close($conn);
					}
				?>
                
				<form name="uploader" method="post" enctype="multipart/form-data" action="">
				<input type="file" name="image">
				<input name="Submit" type="submit" value="Upload">
				</form>
                
 			</div>
    </div>
    </div>
    
    <div id="footer" align="center">
    <p>Copyright &copy; 2011 by <a href="http://www.puffys.net">Kaizokupuffball</a></p>
    <p>You can contact the owner of this site by <a href="mailto:kaizokupuffball@gmail.com">mail</a></p>
    </div>
</body>
</html>

Recommended Answers

All 2 Replies

Line 85:

$copied = copy($_FILES['image']['tmp_name'], $newname);

Use below instead of above.

$copied = move_uploaded_file($_FILES['image']['tmp_name'], $newname);

Line 85:

$copied = copy($_FILES['image']['tmp_name'], $newname);

Use below instead of above.

$copied = move_uploaded_file($_FILES['image']['tmp_name'], $newname);

That worked!
I thought i said that yesterday but i obviously did'nt.
But thank you! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.