Jump to content

Storing uploaded image path in database


DarkPrince2005

Recommended Posts

Hi guys & gals

 

I'm trying to upload an image to a folder, and at the same time store that path in the database, but i'm having trouble storing the file  name.. Please help.

 

Upload form

<html>
<body><form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form></body>
</html>

 

Upload script

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/bmp")
|| ($_FILES["file"]["type"] == "image/gif"))
&& ($_FILES["file"]["size"] < 200000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
mysql_connect("localhost","root","");
mysql_select_db("pix");
mysql_query("insert into pix values('',\"upload/$_FILES[file][name]\")");

if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?> 

 

the  above code though works but it stores the path as 'upload/Array[name]'

Link to comment
https://forums.phpfreaks.com/topic/105323-storing-uploaded-image-path-in-database/
Share on other sites

Just for the future, if you want to include array vars in an sql statement, you either have to come out of the quotes or enclose the value in brackets

 

mysql_query("insert into pix values('','upload/".$_FILES['file']['name']."')");

or

mysql_query("insert into pix values('','upload/{$_FILES['file']['name']}')");

 

Ray

Ha-ha... thank you.  But can you maybe also advise me on how i would change the upload script to, if the filename exists that it should add a 1 or something to the filename.

 

I would guess that'll involve fread(), fopen() or something like that, which i have absolutely no clue about.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.