1

i have problem on my php file upload script , i am trying to upload a file , then store a location on mysql database.

the problem is when i am trying to upload any file , the action.php doesn't give me any message whether it success or failed !

here is my html form:

<form enctype="multipart/form-data" action="add.php" method="POST"> 
 Name: <input type="text" name="name"><br> 
 E-mail: <input type="text" name = "email"><br> 
 Phone: <input type="text" name = "phone"><br> 
 Photo: <input type="file" name="photo"><br> 
 <input type="submit" value="Add"> 
 </form>

here is my add.php code:

     <?php 

 //This is the directory where images will be saved 
 $target = "/images/"; 
 $target = $target . basename( $_FILES['photo']['name']); 

 //This gets all the other information from the form 
 $name=$_POST['name']; 
 $email=$_POST['email']; 
 $phone=$_POST['phone']; 
 $pic=($_FILES['photo']['name']); 

 // Connects to your Database 
 mysql_connect("localhost", "root", "123123") or die(mysql_error()) ; 
 mysql_select_db("bom") or die(mysql_error()) ; 

 //Writes the information to the database 
 mysql_query("INSERT INTO `bom` VALUES ('$name', '$email', '$phone', '$pic')") ; 

 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
 { 

 //Tells you if its all ok 
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
 } 
 else { 

 //Gives and error if its not 
 echo "Sorry, there was a problem uploading your file."; 
 } 
 ?>

mysql conntection is working 100%, i have make sure of that , also i have a folder called " images " on my script folder , where is the problem ?

5
  • What does basic debugging tell you? What does $_POST contain (do a print_r($_POST);? Are there any errors from the query (use echo mysql_error() to see them? Is error reporting activated (use error_reporting(E_ALL); to make sure it is)? Commented Oct 19, 2011 at 17:30
  • Also, your database query is susceptible to SQL injection php.net/manual/en/security.database.sql-injection.php Commented Oct 19, 2011 at 17:34
  • tried , but still nothing , the problem i only got a white page with no message , and the file is not on the images folder Commented Oct 19, 2011 at 17:34
  • I'm fairly sure you don't have error reporting activated. Commented Oct 19, 2011 at 17:35
  • now it says Fatal error: Call to undefined function mysql_connect() in C:\AppServ\www\PromBom\add.php on line 17 Commented Oct 19, 2011 at 17:42

1 Answer 1

1

You've assumed that the file upload actually worked. You should have at least some minimal error handling before you do anything with the upload:

if ($_FILES['uploadedfile']['error'] !== UPLOAD_ERR_OK) {
    die("Upload failed with error code " . $_FILES['uploadedfile']['error'])
}

The error codes are document here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.