萌新求教 在head first php遇到问题 求大佬来看看

扛着小板凳 2018-11-05 09:54:06
下图是index.php 页面功能是显示上传到本地服务器的图片,分数,名字
GW_UPLOADPATH是定义的路径
screenshot是图片名
但是无法显示图片
...全文
83 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
下雨的声音丶 2018-11-07
  • 打赏
  • 举报
回复
我记得之前好像是可以访问本地的路径的,我刚试了不行了,可能我记错了 你这样要访问 你就配置一个E:/images 为跟目录的 域名 echo '<img src="' . $GW_UPLOADPATH . $screenshot . '" alt="Score image" /></p>'; 这里把 $GW_UPLOADPATH 改成你配置的域名就可以
xuzuning 2018-11-07
  • 打赏
  • 举报
回复
是的
扛着小板凳 2018-11-07
  • 打赏
  • 举报
回复
不好意思 我第一次发帖 是直接结帖就好了么?
扛着小板凳 2018-11-07
  • 打赏
  • 举报
回复
引用 7 楼 xuzuning 的回复:
浏览时,图片路径是从网站根目录算起的
你的 E:/images/ 是文件系统目录,不是网站目录

对的 我已经找到问题了 因为我把images建在了错误的地方 才一直move file wrong ,D:\xampp\htdocs\myphp\myphp\myphp\guitar\images建在这里就成功显示图片了 (我要找个方法简化下。。。。)(不过当前项目要是没有images文件夹 就会出错,我还以为它会自己新建一个images文件夹呢)

引用 8 楼 qq_23033339 的回复:
我记得之前好像是可以访问本地的路径的,我刚试了不行了,可能我记错了

你这样要访问 你就配置一个E:/images 为跟目录的 域名
echo '<img src="' . $GW_UPLOADPATH . $screenshot . '" alt="Score image" /></p>';
这里把 $GW_UPLOADPATH 改成你配置的域名就可以
谢谢你的回复 我已经找到方法了 修改域名的方法我会尝试一下的 路径这么长我看着也头晕
xuzuning 2018-11-06
  • 打赏
  • 举报
回复
浏览时,图片路径是从网站根目录算起的
你的 E:/images/ 是文件系统目录,不是网站目录
下雨的声音丶 2018-11-06
  • 打赏
  • 举报
回复
$GW_UPLOADPATH='E:\images\';
扛着小板凳 2018-11-06
  • 打赏
  • 举报
回复
//index.php
<meta />
<title>Guitar Wars - High Scores</title>
<link/>


<h2>Guitar Wars - High Scores</h2>
<p>Welcome, Guitar Warrior, do you have what it takes to crack the high score list? If so, just <a href="addscore.php">add your own score</a>.</p>
<hr />

<?php
require_once('connectvars.php');
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

// Retrieve the score data from MySQL
$query = "SELECT * FROM guitarwars";
$data = mysqli_query($dbc, $query);

// Loop through the array of score data, formatting it as HTML
echo '<table>';
while ($row = mysqli_fetch_array($data)) {
// Display the score data
echo '<tr><td class="scoreinfo">';
echo '<span class="score">' . $row['score'] . '</span><br />';
echo '<strong>Name:</strong> ' . $row['name'] . '<br />'; //上传的名字
echo '<strong>Date:</strong> ' . $row['data'] . '</td>'; //上传的日期
if (is_file(@$GW_UPLOADPATH . $row['screenshot']) && filesize($GW_UPLOADPATH . $row['screenshot']) > 0) {
echo '<td><img src="' . $GW_UPLOADPATH . $row['screenshot'] . '" alt="Score image" /></td></tr>';
}
else {
echo '<td><img src="' . @$GW_UPLOADPATH . 'unverified.gif' . '" alt="Unverified score" /></td></tr>';
}


}
echo '</table>';

mysqli_close($dbc);
?>


//addscore.php

<h2>Guitar Wars - Add Your High Score</h2>

<?php
require_once('connectvars.php');

$GW_UPLOADPATH='E:/images/';

if (isset($_POST['submit'])) {
// Grab the score data from the POST
$name = $_POST['name'];
$score = $_POST['score'];
$screenshot= $_FILES['screenshot']['name'];

if (!empty($name) && !empty($score) && !empty($screenshot)) {
// Connect to the database
$target = $GW_UPLOADPATH. $screenshot;
if (move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)) {
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

// Write the data to the database
$query = "INSERT INTO guitarwars VALUES (0, NOW(), '$name', '$score','$screenshot')";
mysqli_query($dbc, $query);

// Confirm success with the user
echo '<p>Thanks for adding your new high score!</p>';
echo '<p><strong>Name:</strong> ' . $name . '<br />';
echo '<strong>Score:</strong> ' . $score . '</p>';
echo '<img src="' . $GW_UPLOADPATH . $screenshot . '" alt="Score image" /></p>';
echo '<p><a href="index.php"><< Back to high scores</a></p>';

// Clear the score data to clear the form
$name = "";
$score = "";


mysqli_close($dbc);
}
}
else {
echo '<p class="error">Please enter all of the information to add your high score.</p>';
}
}
?>

<hr />
<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="MAX_FILE_SIZE" VALUE="32768">
<label for="name">Name:</label>
<input type="text" id="name" name="name" value="<?php if (!empty($name)) echo $name; ?>" />
<br />
<label for="score">Score:</label>
<input type="text" id="score" name="score" value="<?php if (!empty($score)) echo $score; ?>" />
<br/>
<label for="screenshot">screenshot :</label>
<input type="file" id="screenshot" name="screenshot">
<hr />
<input type="submit" value="Add" name="submit" />
</form>

扛着小板凳 2018-11-06
  • 打赏
  • 举报
回复
引用 3 楼 qq_23033339 的回复:
$GW_UPLOADPATH='E:\images\';

谢谢回复 但是会出现 expected:semicolon 错误 ,所有我换成了$GW_UPLOADPATH='E:/images/';
却还是显示不了图片 我把代码贴上来
扛着小板凳 2018-11-06
  • 打赏
  • 举报
回复
引用 3 楼 qq_23033339 的回复:
$GW_UPLOADPATH='E:\images\';

谢谢回复 但是使用后$GW_UPLOADPATH='E:\images\'; 会显示expected:semicolon
所以我改成了$GW_UPLOADPATH='E:/images/'; 还是无法显示图片。我把代码贴上来
//index.php
<meta />
<title>Guitar Wars - High Scores</title>
<link/>


<h2>Guitar Wars - High Scores</h2>
<p>Welcome, Guitar Warrior, do you have what it takes to crack the high score list? If so, just <a href="addscore.php">add your own score</a>.</p>
<hr />

<?php
require_once('connectvars.php');
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

// Retrieve the score data from MySQL
$query = "SELECT * FROM guitarwars";
$data = mysqli_query($dbc, $query);

// Loop through the array of score data, formatting it as HTML
echo '<table>';
while ($row = mysqli_fetch_array($data)) {
// Display the score data
echo '<tr><td class="scoreinfo">';
echo '<span class="score">' . $row['score'] . '</span><br />';
echo '<strong>Name:</strong> ' . $row['name'] . '<br />'; //上传的名字
echo '<strong>Date:</strong> ' . $row['data'] . '</td>'; //上传的日期
if (is_file(@$GW_UPLOADPATH . $row['screenshot']) && filesize($GW_UPLOADPATH . $row['screenshot']) > 0) {
echo '<td><img src="' . $GW_UPLOADPATH . $row['screenshot'] . '" alt="Score image" /></td></tr>';
}
else {
echo '<td><img src="' . @$GW_UPLOADPATH . 'unverified.gif' . '" alt="Unverified score" /></td></tr>';
}


}
echo '</table>';

mysqli_close($dbc);
?>


//addscore.php

<h2>Guitar Wars - Add Your High Score</h2>

<?php
require_once('connectvars.php');

$GW_UPLOADPATH='E:/images/';

if (isset($_POST['submit'])) {
// Grab the score data from the POST
$name = $_POST['name'];
$score = $_POST['score'];
$screenshot= $_FILES['screenshot']['name'];

if (!empty($name) && !empty($score) && !empty($screenshot)) {
// Connect to the database
$target = $GW_UPLOADPATH. $screenshot;
if (move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)) {
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

// Write the data to the database
$query = "INSERT INTO guitarwars VALUES (0, NOW(), '$name', '$score','$screenshot')";
mysqli_query($dbc, $query);

// Confirm success with the user
echo '<p>Thanks for adding your new high score!</p>';
echo '<p><strong>Name:</strong> ' . $name . '<br />';
echo '<strong>Score:</strong> ' . $score . '</p>';
echo '<img src="' . $GW_UPLOADPATH . $screenshot . '" alt="Score image" /></p>';
echo '<p><a href="index.php"><< Back to high scores</a></p>';

// Clear the score data to clear the form
$name = "";
$score = "";


mysqli_close($dbc);
}
}
else {
echo '<p class="error">Please enter all of the information to add your high score.</p>';
}
}
?>

<hr />
<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="MAX_FILE_SIZE" VALUE="32768">
<label for="name">Name:</label>
<input type="text" id="name" name="name" value="<?php if (!empty($name)) echo $name; ?>" />
<br />
<label for="score">Score:</label>
<input type="text" id="score" name="score" value="<?php if (!empty($score)) echo $score; ?>" />
<br/>
<label for="screenshot">screenshot :</label>
<input type="file" id="screenshot" name="screenshot">
<hr />
<input type="submit" value="Add" name="submit" />
</form>

扛着小板凳 2018-11-05
  • 打赏
  • 举报
回复
而且 神奇的是
注释掉 addscore.php 里的$GW_UPLOADPATH='E:\images'; 后
图片竟然正常显示了 为啥? 附下图
扛着小板凳 2018-11-05
  • 打赏
  • 举报
回复
下面是addscore.php
红圈这里路径这样填对么
这样填的话 图片不知道为什么会存在E:\ 里 而不是E:\images 里 而且 图片的名字前面会加上images
我现在遇到的问题是 图片可以上传到本地服务器,但是不能显示图片,求大佬解答!!

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧