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>
Jonathon Legg
  • 15
  • 2
  • 8

3 Answers3

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.

  • 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. – Populus Jan 25 '12 at 23:04
  • Thanks for your comment, Unfortunately I have to stick with this way for my criteria... :( – Jonathon Legg Jan 25 '12 at 23:04
  • @JonathonLegg are you trying to store the image IN the databse or in a folder on your server? – Paul Dessert Jan 25 '12 at 23:10
  • I am trying to store the image in the folder on my server and the link is put into the database of the image. – Jonathon Legg Jan 25 '12 at 23:17
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.

Community
  • 1
  • 1
Alfabravo
  • 7,493
  • 6
  • 46
  • 82
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?

Populus
  • 7,470
  • 3
  • 38
  • 54
  • 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. – Jonathon Legg Jan 25 '12 at 23:20
  • 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. – Jonathon Legg Jan 25 '12 at 23:21
  • does the directory `second` exist? – Paul Dessert Jan 25 '12 at 23:21
  • definitely a problem with paths... try doing what I did above for $target just you have an absolute path – Populus Jan 25 '12 at 23:24
  • Populus, I think i've found the problem, could be because permissions are not set to 777, will just check this mate. – Jonathon Legg Jan 25 '12 at 23:28
  • tbh I personally dislike 777 permissions, it *can* open up your site to attacks. 775 or 770 is better or even 755 or 750 or 700 :P just make sure the user apache is running on has access to the folder – Populus Jan 25 '12 at 23:30
  • And no that still is not the problem :( AWW im going to get upset haha – Jonathon Legg Jan 25 '12 at 23:31
  • did you try using an absolute path for $target? – Populus Jan 25 '12 at 23:31
  • Tried using a absolute path for target and nothing different, does not like this file for some reason. – Jonathon Legg Jan 25 '12 at 23:38
  • Tried a easier file name, but nope! Doesnt work, Going to hit the sheets hopefully someone may have me a answer or work around by the morning! Thanks for your help mate! goodnight – Jonathon Legg Jan 25 '12 at 23:45
  • Could anyone please help me with my PHP issue regarding uploading images and storing in a folder. – Jonathon Legg Jan 26 '12 at 12:31
  • Yes mate, still not uploading correctly, the code to me seems to be fine, I am going to look over the Form in relation to the local variables and see if they match, something is not working correctly here. – Jonathon Legg Jan 27 '12 at 11:37
  • A small change in code was required, i used it without a fixed URL the image is uploading and the code is successfully being entered into the database. Thanks for all your help, only took me ABOUT 5 hours :( much appreciated mate. – Jonathon Legg Jan 27 '12 at 13:04