我的图片上传表单效果很好。保存数据库的名称和路径。我想将完整的URL保存到图像沿着白名而不是名称。我的意思是现在在DB中保存file_name.jpg
我要保存http://example.com/images/file_name.jpg
。这是upload.php
define('MAX_FILE_SIZE', 20000000430);
$uploadDir = "../../img/";
$permitted = array('image/jpeg', 'image/jpeg', 'image/png', 'image/gif');
$fileName = $_FILES['image']['name'];
$tmpName = $_FILES['image']['tmp_name'];
$fileSize = $_FILES['image']['size'];
$fileType = $_FILES['image']['type'];
// make a new image name
$ext = substr(strrchr($fileName, "."), 1);
// generate the random file name
$randName = md5(rand() * time());
// image name with extension
$myFile = $randName . '.' . $ext;
// save image path
$path = $uploadDir . $myFile;
if (in_array($fileType, $permitted))
{
$result = move_uploaded_file($tmpName, $path);
if (!$result)
{
echo "Error uploading image file";
exit;
}
else
{
// keep track post values
$name = $_POST['name'];
$description = $_POST['description'];
// update data
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE food set name = ?, image = ?, path = ?, description = ?
WHERE id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($name,$myFile,$path,$description,$id));
Database::disconnect();
echo "<code>Information updated!!</code>";
}
}
我尝试将URL放在$ uploadDir中。
$uploadDir = "http://example.com/img/";
但是我收到了这个错误。
Warning: move_uploaded_file(http://example.com/img/75a13564a8f3305fb0a30ab95487b8de.jpg): failed to open stream: HTTP wrapper does not support writeable connections
也试过这样的事情并得到同样的错误
define('domainURL', 'http://'.$_SERVER['HTTP_HOST']);
$path = domainURL . $uploadDir . $myFile;
答案 0 :(得分:2)
move_uploaded_file函数不接受文件网址。
它接受图像实际路径。
因为,您的文件正在物理移动。
e.g。 $path = $_SERVER['DOCUMENT_ROOT'] . 'img/'
答案 1 :(得分:1)
您可以使用相对或绝对路径。 相对路径是“../../img/”; 绝对路径应该像“/ www / htdocs / img /”; (你可以在FTP客户端看到绝对路径) 而你不能使用URL。 对于DB中的商店,使用另一个带URL的变量
答案 2 :(得分:0)
在php变量中分配上传路径,并使用$ path将其存储在数据库中..
$path = "www.sitename.com/uploads/".$filename ;