I have the following php which uploads image to server with name inserted in database:
<?php
//Uploading File to server php folder//
$uploaddir = ''; //Uploading to same directory as PHP file
$file = basename($_FILES['userfile']['name']);
$uploadFile = $file;
$randomNumber = rand(0000, 99999);
$newName = $uploaddir . $randomNumber . $uploadFile;
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
echo "Temp file uploaded. \r\n";
}
else {
echo "Temp file not uploaded. \r\n";
}
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $newName)) {
$postsize = ini_get('post_max_size');
$canupload = ini_get('file_uploads');
$tempdir = ini_get('upload_tmp_dir');
$maxsize = ini_get('upload_max_filesize');
echo "http://localhost/abc/images/{$newName}" . "\r\n" . $_FILES['userfile']['size'] . "\r\n" . $_FILES['userfile']['type'] ;
}
//Making sql db connection to store image path in db table//
$host = "localhost";
$username = "root";
$password = "****";
$database = "userauth";
mysql_connect($host, $username);
mysql_select_db($database) or die("Unable to find database");
$image = $_GET["images"];
$qry = "INSERT INTO image VALUES ('','$newName')";
mysql_query($qry);
mysql_close();
?>
The above code easily uploads files with temporary name: upload_image prepended by random number to the following xampp folder path: http://localhost/abc/images/
In the db table, i rev the image name as ‘randomnumber’upload_image.jpg.
What I need is that I need the db table to display image name along with the full path as http://localhost/abc/images/‘rand.number’upload_image.jpg
How am I able to achieve this?
I tried to replace uploaddir with “http://localhost/abc/images/” and tried to add it to the new file name but it returned the following warning:
<b>Warning</b>: move_uploaded_file(http://localhost/abc/images/33760upload_image.jpg) [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: failed to open stream: HTTP wrapper does not support writeable connections in <b>/Applications/XAMPP/xamppfiles/htdocs/xampp/abc/images/imageupload.php</b> on line <b>24</b><br />
<br />
<b>Warning</b>: move_uploaded_file() [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: Unable to move '/Applications/XAMPP/xamppfiles/temp/phpdlCc5N' to 'http://localhost/abc/images/33760upload_image.jpg' in <b>/Applications/XAMPP/xamppfiles/htdocs/xampp/abc/images/imageupload.php</b> on line <b>24</b>
<br />
Valuable guidance required.
PS: PERMISSIONS ARE SET TO READ AND WRITE
Table contains two fields: ID and Image with ID set to primary and auto increment
http://php.net/manual/en/reserved.variables.server.php
with the
$_SERVERarray you can access some http request details, e.g. the pathyou 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.