Using PHP to upload file and add the path to MySQL database
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
How to store file path using PHP file upload
You should try something like this :
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="test"; // Database name
//$tbl_name="members"; // Table name
$con=mysql_connect("localhost","root","");
if(! $con)
{
die('Connection Failed'.mysql_error());
}
mysql_select_db("test123",$con);
if(isset($_FILES['files'])){
$errors= array();
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
$file_name = $key.$_FILES['files']['name'][$key];
$file_size =$_FILES['files']['size'][$key];
$file_tmp =$_FILES['files']['tmp_name'][$key];
$file_type=$_FILES['files']['type'][$key];
$file_path=$_FILES['files']['path'][$key];
if($file_size > 2097152){
$errors[]='File size must be less than 2 MB';
}
//$desired_dir=$options['upload_dir']."user_data";
$desired_dir="user_data";
if(empty($errors)==true)
{
if(is_dir($desired_dir)==false){
mkdir("$desired_dir", 755); // Create directory if it does not exist
}
$file_path_name = "$desired_dir/".$file_name;
if(is_dir("$desired_dir/".$file_name)==false){
move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
}else{ // rename the file if another one exist
$new_dir="$desired_dir/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
$query="INSERT into upload(`FILE_NAME`,`FILE_SIZE`,`FILE_TYPE`,`FILE_PATH`) VALUES('$file_name','$file_size','$file_type', '$file_path_name'); ";
mysql_query($query);
}else{
print_r($errors);
}
}
if(empty($error)){
echo "Success";
}
}
?>
Using PHP to upload file add path to MySQL
Maybe you did't set the enctype
attribute for your Form. Without it, php won't be able to find the file. <form method="POST" action="" enctype="multipart/form-data">
should work for you. I have something similar:<form action="" enctype="multipart/form-data" method="POST" >
<input type="hidden" name="MAX_FILE_SIZE" value="8000000">
<input type="file" name="image" />
<br>
<input type="text" name="caption" placeholder="Enter Caption">
<inputtype="submit" name="submit" value="Upload" />
</form>
And it works for me.
Trying to upload files to a folder and to the database
Are there no errors displayed?
Have you tried to display the contents of the $insert variable with a var_dump then copy / paste the SQL query into your mysql executor (ex: phpmyadmin)?
I think you want to test the $result_insert and not the $insert in this part:
if($insert){
$statusMsg = "The file ".basename($_FILES['lfile']['name']). " has been uploaded successfully.";
}
else{
$statusMsg = "File upload failed, please try again.";
}
How to move an uploaded file and upload the path into mySQL database? Using PHP
if you want the path to be saved in db you should use this -
<?php
//database connected
$id = $_POST['id'];
if (isset($_FILES['file']['tmp_name'])){
$file = $_FILES['file']['tmp_name'];
$filename = $_FILES['file']['name'];
$folder="/uc webserver formulier/root/uploads/";
move_uploaded_file($file,$folder.$filename);
$handle = $folder.$filename ;
}
$query= "UPDATE config SET showimage = '$handle' WHERE id='$id'";
if (mysql_query($query)){
echo "updated";
header('Refresh:1;url=admin.php');}
else{
echo "fail";}
// Close connection
mysql_close($db);
?>
upload file with php and save path to sql
To upload a file you need at least a HTML POST form with multipart/form-data
encoding. Therein you put an input type="file"
field to browse the file and a submit button to submit the form.
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit">
</form>
In the upload.php
the uploaded file is accesible by $_FILES
with the field name as key.
$file = $_FILES['file'];
You can get its name as follows:
$name = $file['name'];
You need to move it to a permanent location using move_uploaded_file()
, else it will get lost:
$path = "/uploads/" . basename($name);
if (move_uploaded_file($file['tmp_name'], $path)) {
// Move succeed.
} else {
// Move failed. Possible duplicate?
}
You can store the path in database the usual way:
$sql = "INSERT INTO file (path) VALUES ('" . mysqli_real_escape_string($path) . "')";
// ...
Related Topics
How to Build Unlimited Level of Menu Through PHP and MySQL
Request Headers Bag Is Missing Authorization Header in Symfony 2
PHP Regular Expression for Strong Password Validation
PHP 5.4 - 'Closure $This Support'
How to Run a PHP Without a Web Server
Convert Jpg/Gif Image to Png in PHP
Wamp/MySQL Errors Not in Correct Language
How to Convert Seconds to Time Format
How to Pass Get Parameters to Laravel from with Get Method
When *Not* to Use Prepared Statements
How to Display Unicode Data with PHP
PHP - Plus Sign with Get Query
PHP Filesize Reporting Old Size
Check If a Row Exists Using Old MySQL_* API