Upload.php:
<?php
//This is the directory where images will be saved
$target = "pics";
$target = $target . basename( $_FILES['Filename']['name']);
//This gets all the other information from the form
$Filename=$_POST['Filename'];
$Description=$_POST['Description'];
$pic=($_FILES['Filename']['name']);
// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("altabotanikk") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO picture (Filename,Description)
VALUES ('$Filename', '$Description')") ;
//Writes the Filename to the server
if(move_uploaded_file($_FILES['Filename']['tmp_name'], $target)) {
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['Filename']). " 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.";
}
?>
And here is the form(in a separate file):
<form method="post" action="upload.php" enctype="multipart/form-data">
<p>Photo:</p>
<input type="file" name="Filename">
<p>Description</p>
<textarea rows="10" cols="35" name="Description"></textarea>
<br/>
<input TYPE="submit" name="upload" value="Add"/>
</form>
The errors are
Undefined index: Filename on Line 17
(the $Filename=$_POST['Filename'];)
and
Undefined index: uploadedfile on Line 35
(the echo "The file ". basename( $_FILES['uploadedfile']['Filename']). " has been uploaded, and your information has been added to the directory";)
echo"<pre>".print_r($_FILES,true)."</pre>";
gives me:
Array
(
[Filename] => Array
(
[name] => Laserkanon.jpg
[type] => image/jpeg
[tmp_name] => C:\WampServer\tmp\php11D4.tmp
[error] => 0
[size] => 41813
)
)
In this case, I have used PHP to upload the file to a directory and save the path of the file in the MySQL database. Basically, you will need a simple HTML form and a PHP code to upload the file. So, here’s the HTML form.
Create a new PHP project folder and call it file-upload-download. Create a subfolder inside this folder called uploads (this is where our uploaded files will be stored), and a file called index.php. index.php is where we will create our file upload form. Open it and put this code inside it:
We don't put the file directly in the MySQL database. We upload it to a directory on our site. We simply get the file name and place it in the MySQL database. We then have the complete pathway to the file in our code so that we can show and display it. So if we're saving a file named, mortgage.pdf, we save the full file name, mortgage.pdf.
We then can, using PHP code, show the file by specifying the full path to that file. This is a much, much easier, and simple way of going about it then to actually upload files into the database, which is more complex.
First you should use print_r($_FILES)
to debug, and see what it contains. :
your uploads.php
would look like:
//This is the directory where images will be saved
$target = "pics/";
$target = $target . basename( $_FILES['Filename']['name']);
//This gets all the other information from the form
$Filename=basename( $_FILES['Filename']['name']);
$Description=$_POST['Description'];
//Writes the Filename to the server
if(move_uploaded_file($_FILES['Filename']['tmp_name'], $target)) {
//Tells you if its all ok
echo "The file ". basename( $_FILES['Filename']['name']). " has been uploaded, and your information has been added to the directory";
// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("altabotanikk") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO picture (Filename,Description)
VALUES ('$Filename', '$Description')") ;
} else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
EDIT: Since this is old post, currently it is strongly recommended to use either mysqli or pdo instead mysql_ functions in php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With