You are viewing our Forum Archives. To view or take place in current topics click here.
[PHP] File Upload Help
Posted:

[PHP] File Upload HelpPosted:

MLP
  • TTG Contender
Status: Offline
Joined: Oct 26, 201013Year Member
Posts: 3,869
Reputation Power: 177
Status: Offline
Joined: Oct 26, 201013Year Member
Posts: 3,869
Reputation Power: 177
I want to be able to upload an image to a directory, and then store the filepath into a database, but no matter how many tutorials I have followed, it just isn't working.

If anyone could help me, or point me to a good tutorial, I would really appreciate it.

Thanks.
#2. Posted:
iyop45
  • Prospect
Status: Offline
Joined: Apr 15, 201113Year Member
Posts: 614
Reputation Power: 83
Status: Offline
Joined: Apr 15, 201113Year Member
Posts: 614
Reputation Power: 83
Ok, well Unfortunately ttg doesn't allow me to post html code? But, assuming you have a form with say just a file upload. Adding other fields only requires a slight change of the php script and the html.
(I can't use tags so just bear with me here)
Use input masks with the following allocated:

/form method="POST" action="script.php" enctype="multipart/form-data"/
/input/ type="text" name="title"/
/input/ type="file" name="image"/
/form/

The "script.php":
<?php
/* This will include all the configuration details to allow the script to connect to your database */
include="db_config.php";

//This is the directory where images will be saved
 $path = "/images/";
 $cpath = $path . basename( $_FILES['images']);
/* This will be very vulnerable but, I don't want to go much into the security of it */

 $ip = $_SERVER['REMOTE_ADDR'];
/* I like to include the ip of the user too */

 $today = date("Ymd");
// The date it was uploaded

 $image =($_FILES['image']['title']);
// This is the part you need

 mysql_select_db("db_name") or die(mysql_error()) ;

//Writes the information to the database
/* Assuming you have the database in the appropriate format so say "ID","PATH","DATE","IP" */
 mysql_query("INSERT INTO `images` VALUES ( '', '/images/$image', '$today', '$ip')") ;

move_uploaded_file($_FILES['photo']['tmp_name'], $cpath));
echo "Fingers crossed!";
?>


I havn't tested this so cut me some slack if it doesn't work, of cource it is very vulnerable to attacks but, I am trying to keep it as short as possible. if there are any parts missing tell me I'll change and perhaps we can sort it out. That's how you learn rather than being fed a chunk of code.

if there are any problems you can always try the following line:

echo "var_dump($_FILES)";

And it should help you identify problems with the code.

Anyway think I've waffled on a lot and if I havn't helped sort your problem I hope I have put you in the right direction.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.