【发布时间】:2016-09-19 20:11:45
【问题描述】:
所以我希望我的页面显示我从 mysql 数据库获取其路径并显示在同一屏幕上的图像。这是我的代码,我已经尝试了所有方法,请告诉我哪里出错了。
while ($row = mysqli_fetch_array($return_data)) {
echo "ID:".$row['demo_id']."<br>";
echo "Name: ".$row['demo_name']."<br>";
echo "Version: ".$row['demo_version']."<br>";
echo "Details: ".$row['demo_details']."<br>";
echo "File Link: ".$row['file']."<br>";
$new = $row['file'];
echo '<img src = \"$new\"/>';
}
mysqli_free_result($return_data);
echo "Data retrieved successfully!"."<br>";
?>
<img src = "<?php echo $new?>">
echo "File Link:" 返回上传文件的完整路径。
如何在同一页面中呈现该路径的图像?
两个图像标签都不起作用。提前致谢!
编辑
文件链接:C:/Apache24/htdocs/demo_webpages_project/neweruploads/footer.jpg
这是我作为输出得到的路径。
基本上这是我从另一个 php 文件上传图像的文件夹
<?php
//this module is used to temporarily store the uploaded file to the server
$target_dir = "random/"; //we randomly assign a value to the upload target directory
$target_file = $target_dir . basename($_FILES["image_file"]["name"]); /*here ["name"] is the original name of the file before it was updated
target file is assigned this name by appending it to the $targer_dir
now target_file is the uploaded file name along with the path*/
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);/*this returns various kind of info about the path for eg Directory, Basename
in our case it returns extension of the file*/
//this sub module is to check whether the file is really an image file
if(isset($_POST["submit"])) { //isset is used to confirm whether the value has actually being submitted
$check = getimagesize($_FILES["image_file"]["tmp_name"]);/*here ["tmp_name"] is the location of temporary file on server
getimagesize confirms image format by returning dimensions etc*/
if($check !== false) {
echo "A file is of an image format<br>";
}
else {
echo "The file is not an image!<br>";
}
}
//Test module to upload files to a destination directory and check whether they have been uploaded or not
if (is_uploaded_file($_FILES['image_file']['tmp_name']) && $_FILES['image_file']['error']==0) { /*code to check two things: 1. whether the file exists in the temp memory 2. whether the file has any error*/
$path = 'C:\Apache24\htdocs\demo_webpages_project\neweruploads\\' . $_FILES['image_file']['name']; /*this sets the destination directory(along with expected file name)*/
if (!file_exists($path)) { //if the file does not exists at that path
if (move_uploaded_file($_FILES['image_file']['tmp_name'], $path)) { //use the php file move function to move it
echo "The file was uploaded successfully."; //success
}
else {
echo "The file was not uploaded successfully."; //failure
}
}
else {
echo "File already exists. Please upload another file."; //detects existence of file with exact same name
}
}
else {
echo "The file was not uploaded successfully."; //if any problem with original uploading
echo "(Error Code:" . $_FILES['image_file']['error'] . ")";
}
?>
这有帮助吗?
编辑 2
http://localhost:8080/demo_webpages_project/download.php?project_name=footer&version=&submit=Search
这是我的本地目录路径。
您提供的解决方案允许我读取 demo_webpages_project 文件夹中直接指向那里的图像),而不是 neweruploads 文件夹
【问题讨论】:
-
试试这个
echo "<img src='".$new."' />"; -
whole path?如/home/sites/example.com/html/uploads/kittens.jpg?那永远行不通。您必须输出 URL,而不是文件系统路径。你需要<img src="/uploads/kittens.jpg"> -
@JoseManuelAbarcaRodríguez 我已添加编辑,请检查。
-
@MarcB 我明白了!有什么办法可以提取网址?