0

yet another PHP question which I am stuck on, so here is what im trying to do and my code:

Step one - Attempting to upload a Name, Image and ID to a SQL Table. The table is called second and has 4 fields. I am using one of the fields as a FK from another table called admin and the FK is id.

What is working? *When i click the submit button, the link of the image gets uploaded, but physically no image within my folder. When trying to upload the form I am wanting to use the ID of the user and put that into the new table so i can refer to that field to display the data. I feel like this is a hard scenario to explain*

Edits here -Realised that Id field, favname field, are not recording any information, and link is not uploading to the folder correctly.

Any help would be much appreciated: Here goes.

SecondPic.php

<?php
include "common.php";
$secondid = $_GET['id'];
DBConnect();



$Link = mysql_connect($Host, $User, $Password);

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


$favname = $_POST["name1"];
$pic2=($_FILES['photo1']['name']); 
$id = $_POST["$formID"];



$Query ="INSERT into $Table_2 values ('0', '$id', '$favname', '$pic2')";

if (mysql_db_query ($DBName, $Query, $Link)){
print ("A record was created <br><a href=index.php> return to index </a>\n");

 // Connects to your Database 
 //mysql_connect("localhost", "jonathon_admin", "hello123") or die(mysql_error()) ; 
 //mysql_select_db("jonathon_admin1") or die(mysql_error()) ; 


 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['photo1']['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."; 
 } 


} else {

print (" - Your Record was not created");   
}

mysql_close($Link);
?>

<!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>Untitled Document</title>
</head>

<body>
</body>
</html>

SecondUpload.php

<!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>Untitled Document</title>
</head>

<body>

<form enctype="multipart/form-data" id="form1" name="form1" method="post" action="secondPic.php">
  <p>
    <label for="name1">Fav Location Name: </label>
  <input type="text" name="fav1" id="fav1" />
  </p>
  <p>
  <label for="photo1">Fav Location Photo: </label>
 <input type="file" name="photo1"><br> 
  </p>
  <p>
  <label for="formID">ID: <? echo $rows['id']; ?> </label>
  <input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>">
  </p>
  <p>
    <input type="submit" name="submit" id="submit" value="Submit" />
  </p>
</form>



</body>
</html>
1
  • does "second/" exist on your server? Commented Jan 25, 2012 at 23:10

3 Answers 3

1

Having worked with this before, my biggest recommendation is not to store images (or really any binary data) in a Database. It leads to many issues which will cause issues in the future. Instead, I recommend storing the file locally and keeping a relative path to the object.

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

4 Comments

This is a debate that has not had an conclusion yet, so don't jump the gun and declare one side the winner :P I can still see many advantages to using DB to store images. So it all comes down to the requirements of the project.
Thanks for your comment, Unfortunately I have to stick with this way for my criteria... :(
@JonathonLegg are you trying to store the image IN the databse or in a folder on your server?
I am trying to store the image in the folder on my server and the link is put into the database of the image.
1

Check these threads about MYSQL storing images as BLOBs.

To Do or Not to Do: Store Images in a Database

Insert Blobs in MySql databases with php

The general recommendation is: do not store images in MySQL as the growth is hard to manage and the performance could degrade. Also, using your images on the fly will require you to transform them back, with an additional cost. There's no better and cheaper file handler than your own server OS.

Comments

0

Aftr Looking at your post in it's entirety, the title has mislead me (and probably the guy above) into think you wanted to store the image data directly into the database.

That is not what you want to do.

Here are my questions for you before we proceed.

$target = "second/"

Is this script "SecondPic.php" in the base folder? And the folder "second" in the same base folder?

Generally it's better to give an absolute path in move_uploaded_file(), so $target should be something like:

$target = dirname(__FILE__) . DIRECTORY_SEPARATOR . $_FILES['photo1']['name'];

Secondly, it's better to move the file before saving to database, because moving the file has a higher chance of erroring out.

Thirdly, any errors? in log or output of page?

13 Comments

Yes they are both in the same "starting directory" - I will check for errors now. Here is what it says,: A record is created indicates that something has been put into the database.
Database Works SuccessfullyA record was created return to index Warning: move_uploaded_file(second/380514_194644347292063_181312801958551_382699_182684041_n.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /web/users/j9061387/ok/secondPic.php on line 32 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpt0GqUl' to 'second/380514_194644347292063_181312801958551_382699_182684041_n.jpg' in /web/users/j9061387/ok/secondPic.php on line 32 Sorry, there was a problem uploading your file.
does the directory second exist?
definitely a problem with paths... try doing what I did above for $target just you have an absolute path
Populus, I think i've found the problem, could be because permissions are not set to 777, will just check this mate.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.