我有以下php将图像上传到服务器,并在数据库中插入名称:
<?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();
?>
上面的代码可以轻松上传临时名称的文件:upload_image以随机编号前置到以下xampp文件夹路径:http://localhost/abc/images/
在db表中,我将图像名称修改为&#39; randomnumber&#39; upload_image.jpg。 我需要的是我需要db表来显示图像名称以及完整路径http://localhost/abc/images/&#39; rand.number&#39; upload_image.jpg
我如何实现这一目标?
我尝试用&#34; http:// localhost / abc / images /&#34;替换uploaddir;并尝试将其添加到新文件名,但它返回以下警告:
<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 />
需要宝贵的指导。
PS:允许读取和写入权限 表包含两个字段:ID和图像,ID设置为主要和自动增量
答案 0 :(得分:1)
http://php.net/manual/en/reserved.variables.server.php
使用$_SERVER
数组,您可以访问某些http请求详细信息,例如路径
您不应该尝试将上传的文件从/移动到像http这样的只读协议。而只是从php tmp目录中获取它。也不要在移动时启动这样的文件名,这会导致你的错误。