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:
You are adding random string to uploaded file using PHP functions.
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.
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
Converting MySQL Result Array to Json
Best Way to Completely Destroy a Session - Even If the Browser Is Not Closed
Running Command-Line Application from PHP as Specific User
Merging Arrays With the Same Keys
Preserve Key Order (Stable Sort) When Sorting With PHP'S Uasort
How to Get Url of Current Page in PHP
Download Files in Laravel Using Response::Download
Generating Cryptographically Secure Tokens
Can File Uploads Time Out in PHP
Append Data to a .Json File With PHP
How to Do Left Join in Doctrine
How to Emulate a Get Request Exactly Like a Web Browser
Show Image Using File_Get_Contents
Fatal Error: Call to a Member Function Prepare() on Null
Multiple MySQL Insert Statements in One Query PHP
File_Get_Contents(): PHP_Network_Getaddresses: Getaddrinfo Failed: Name or Service Not Known