Upload photo

I want to put a picture in a folder with php. First I want to use only php and then to put in a database the file src.
For now I don’t know why I can’t put a picture in the folder called photos.
This is my page:

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

<head>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	<link rel="stylesheet" href="css/default.css" />
	<title>My Photos</title>
</head>

<body>
<?php

$uploads_dir = 'photos';
$name = $_FILES["photo"]["name"];
$tmp_name = $_FILES["photo"]["tmp_name"];
if($name && $tmp_name) {
    copy($tmp_name, "$uploads_dir/$name");
    echo "Success";
}

?>
<h1>Submit Photo</h1>

<p>Hey, Just fill in the form and your photo will be added to this weeks theme! good luck.</p>

<form action="create.php" method="post" enctype="multipart/form-data">
	<fieldset>
	
	     <div>
		     <label><b>Photo Title:</b></label>

		     <input name="photo[title]" size="40" type="text" value="" class="textfield" />
			 </div>
			
			

		   <label><b>Attach Photo</b> </label>
			   <input name="photo" type="file"  /><br/>
				 (e.g. jpg files and not bigger than 1MB )
			 </div> 
      <div>
			

			 
			 <input type="submit" name="submit" value="post photo"  />
			 
	 </fieldset>
</form>	 
</body>
</html>

I tried directly with database and worked. This is the php for the page:

<?php

require 'config_photo.php';
require 'functions.php';
require 'database.php';

if(isset($_FILES['fupload'])) {

    $filename = addslashes($_FILES['fupload']['name']);
    $source = $_FILES['fupload']['tmp_name'];    
    $target = $path_to_image_directory . $filename;
    $description = addslashes($_POST['description']);    
    $src = $path_to_image_directory . $filename;
    $tn_src = $path_to_thumbs_directory  . $filename;    
    
    
    // Validates the form input
    
    if(strlen($_POST['description']) < 4) 
    $error['description'] = '<p class="alert">Please enter a description for your photo. </p>';
    
    if($filename == '' || !preg_match('/[.](jpg)|(gif)|(png)|(jpeg)$/', $filename)) 
    $error['no_file'] = '<p class="alert">Please select an image, dummy! </p>';
    
    if(!$error) {
        move_uploaded_file($source, $target);    
        
        $q = "INSERT into photo(description, src, tn_src) VALUES('$description', '$src', '$tn_src')";
        $result = mysql_query($q) or die(mysql_error());
        
        if($result) {
            echo "Success! Your file has been uploaded";
        }
        
        //createThumbnail($filename);
        
    }  
}     

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

<head>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	<link rel="stylesheet" href="css/default.css" />
	<title>My Photos</title>
</head>

<body>
    <h1>My Photos</h1>
   
    <ul><?php getPhotos(); ?></ul>

    <h1>Upload a Photo</h1>

    <form enctype="multipart/form-data" method="post" action="index_photo.php">
        <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
	    <p><input type="file" name="fupload" /></p>
	    
	    <p><label for="description">Enter a Description: </label>
	    <textarea rows="6" cols="50" id="description" name="description"></textarea></p>
	    
	    <p><input type="submit" value="Upload Photo" name="submit" /></p>
    </form>

    <?php
    if ($error['no_file']) echo $error['no_file'];
    if ($error['description']) echo $error['description'];
    ?>

    <br />
    <a href="deletephoto.php">Delete A Photo</a>

</body>
</html>

with:


$path_to_image_directory = 'photos/';
$path_to_thumbs_directory = 'photos/tn/';

you need to check if any error occured
by checking $_FILES[“photo”][“error”] variable

also, you must be sure that you can see every PHP error message.

Without error checking you can do nothing. As well as any forum folks.

And try echoing the values of $name and $tmp_name if they have expected values or not. And check if the folder/directory ‘photos’ has the write permissions or not. So better try showing all errors as Shrapnel said. I would do something like this:


error_reporting(E_ALL);
if(is_uploaded_file($_FILES["photo"]["tmp_name"])){
	$uploads_dir = 'photos';
	$name = $_FILES["photo"]["name"];
	$tmp_name = $_FILES["photo"]["tmp_name"];
	if(move_uploaded_file($_FILES["photo"]["tmp_name"], $uploads_dir . '/' . $name)){
		echo 'Filed movied successfully.';
	}
	else{
		echo 'There was an error uploading photo.';
	}
}
else{
	echo "Upload a file";
}

Actually one don’t need to check permissions manually.
Because there is possibility to do it in a wrong way and it will lead you in wrong direction.
In other hand, PHP will tell you everything about permissions problem if any. No actions needed.

as for is_uploaded_file, in modern times we use $_FILES[“photo”][“error”] instead
I’d make it something

if(empty($_FILES["photo"]["error"])){
    $uploads_dir = 'photos';
    $name = $_FILES["photo"]["name"];
    if(move_uploaded_file($_FILES["photo"]["tmp_name"], $uploads_dir . '/' . $name)){
        echo 'Filed movied successfully.';
    }
    else{
        echo 'There was an error uploading photo.';
    }
} else {
    trigger_error("Upload, ".$_FILES["photo"]["error"]);
}

To get in touch with error type.

Thank you very much for your help guys. Now works :).

Good to hear that it works. But it would be nice if you mention what was the problem and what you did to make it work because if someone later stuck in similar problem and searches the forum, it will help.

Now I have another problem. Maybe I don’t see the mistake, maybe my logic is wrong, I don’t know, but else statement doesn’t work.

Please someone tell me where am I wrong.


if (isset($_POST['submit'])){

	$question= $_POST['question'];
	$opt1= $_POST['opt1'];
	$opt2= $_POST['opt2'];
	$opt3= $_POST['opt3'];
	$answer= $_POST['answer'];
	
	if(isset($_FILES['photo_src'])){
		$filename = addslashes($_FILES['photo_src']['name']);
		$source = $_FILES['photo_src']['tmp_name'];    
		$target = $path_to_image_directory . $filename;  
		$src = $path_to_image_directory . $filename;
		
			if($filename == '' || !preg_match('/[.](jpg)|(gif)|(png)|(jpeg)$/', $filename)) {
				$error['no_file'] = '<p class="alert">Please select an image, dummy! </p>';
			}
		
		if(!$error) {
			move_uploaded_file($source, $target); 
			$sql = "INSERT INTO quiz (question, opt1, opt2, opt3, answer, photo_src) VALUES ('$question','$opt1','$opt2','$opt3','$answer', '$src')";
			$result = mysql_query($sql);
			
			if($result) {
				echo "Success! Question and picture inserted.<br /> ";
				print_r($_FILES);
				
			} 
		
		}
	}	
	else{
			 
			$sql = "INSERT INTO quiz (question, opt1, opt2, opt3, answer) VALUES ('$question','$opt1','$opt2','$opt3','$answer')";
			$result = mysql_query($sql);
			if($result) {
				echo "Success! Question inserted. ";
				print_r($_POST);
			
			}

	}
	
	
}

it must be PHP who tell you where you’re wrong.
Learn some debugging techniques.
Add some debugging echo’s to see if else statement actually runs
also check your query result. it query fails, there will be no output, as if no else were executed

This condition has to be changed to either my suggestion above or Shrapnel’s suggestion with error checking.


if(isset($_FILES['photo_src'])){


if(is_uploaded_file($_FILES['photo_src']['tmp_name'])){

Because isset will always be true since when you post it will be set with some error messages. See print_r($_FILES) once.

Oh yeah.
Just checked my own code.
I am checking if ($_FILES[‘userfile’][‘name’]) to see if any file were actually uploaded (because user has a choice to upload or not) and then, if any - checking $_FILES[‘userfile’][‘error’] to see if there were any errors.

Thank you. With this line

if(is_uploaded_file($_FILES['photo_src']['tmp_name'])){

now I can insert a text in database if I have a picture, or(with Else) I can insert the text without picture.