How to Store File Name in Database, With Other Info While Uploading Image to Server Using PHP

How to give the uploaded image file a unique filename

You can use PHP's random functions to create random strings for the uploaded file.

mt_rand()

md5()

substr()

So, please modify the line:

$target = "images/".basename($image);

To

$target = "images/". substr(md5(mt_rand(0, 9999)), 0, 21) . '-' . basename($image);

Explanation:

  1. You are adding random string to uploaded file using PHP functions.

  2. Using random strings to rename/save uploaded files fix a severe issue: An image my get overridden if the same image name's file is uploaded.

  3. The uploaded file now has 21 random characters prepended to it. So, there is hardly possibility to have file with same name.

Renaming an image during the uploading PHP Mysql

I would try something like this, you will create a unique id and append the extension of the file to it, if that name exists you loop until you have one that doesn't, then you move the file.

<?php
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");

$dataType = mysql_real_escape_string($_POST["dataType"]);
$title = mysql_real_escape_string($_POST["title"]);

$fileData = pathinfo(basename($_FILES["image"]["name"]));

$fileName = uniqid() . '.' . $fileData['extension'];

$target_path = ($_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $fileName);

while(file_exists($target_path))
{
$fileName = uniqid() . '.' . $fileData['extension'];
$target_path = ($_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $fileName);
}

if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_path))
{
// The file is in the images/gallery folder. Insert record into database by
// executing the following query:
$sql="INSERT INTO images (data_type, title, file_name)"."VALUES('$dataType','$title','$fileName')";
$retval = mysql_query($sql);

echo "The image was successfully uploaded and added to the gallery :) <a href='index.php'>Add another image</a>";


}
else
{
echo "There was an error uploading the file, please try again!";
}

?>

Upload image to server and store image path in mysql database

SQL:

CREATE TABLE `uploads` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`filename` varchar(255) NOT NULL,
`path` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

PHP:

$file_path = "uploads/";

$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
// replace $host,$username,$password,$dbname with real info
$link=mysqli_connect($host,$username,$password,$dbname);
mysqli_query($link,"INSERT INTO `files` (filename,path) VALUES ('".$_FILES['uploaded_file']['tmp_name']."','".$file_path."')") or trigger_error($link->error."[ $sql]");
mysqli_close($link);

} else{
echo "fail";

Database Reference Image Upload Wrong filename stored

First, Add the following configuration line of code to avoide overwrite $config['overwrite'] = false

Second, Give actual values to max_size, max_width and max_height not zero

$config['max_size']     = 1000;
$config['max_width'] = 430;
$config['max_height'] = 430;
$config['overwrite'] = false;

And where the file is uploading do some changes

if(!$this->upload->do_upload('userfile')){
$errors = array('error'=>$this->upload->display_errors());
$post_image = 'noimage.jpg';
}else {
$data = $this->upload->data();
$post_image = $data['file_name'];

}

PHP: Upload an image with image name prepended by HTTP Path?

http://php.net/manual/en/reserved.variables.server.php

with the $_SERVER array you can access some http request details, e.g. the path

you should not try to move the uploaded file from / to an readonly protocol like http. instead just get it from the php tmp directory. also don't start a filename like that while moving it, which causes your errors i think.



Related Topics



Leave a reply



Submit