0

我有以下 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 表中,我将图像名称更改为“randomnumber”upload_image.jpg。我需要的是我需要 db 表将图像名称以及完整路径显示为http://localhost/abc/images/ 'rand.number'upload_image.jpg

我怎样才能做到这一点?

我尝试将 uploaddir 替换为“http://localhost/abc/images/”并尝试将其添加到新文件名中,但它返回了以下警告:

<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 设置为主要和自动增量

4

1 回答 1

1

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

使用$_SERVER数组,您可以访问一些 http 请求详细信息,例如路径

您不应该尝试将上传的文件从 / 移动到像 http 这样的只读协议。而是从 php tmp 目录中获取它。也不要在移动它时开始这样的文件名,我认为这会导致你的错误。

于 2012-04-20T11:59:57.017 回答