我在windows下 安装了apache+mysql+php环境后,进行程序编写和测试。
制作了一个上传图片的页面,发现了一个问题。
如果最终图片的存放路径我按照linux的方式写,图片就无法复制到目录中去。
如果按照windows的方式写,的确可以复制,但是就无法使用<img src=#>的方式显示。下面是我写的程序,请大家帮助解答,谢谢了!注意我加批注的地方
<?php
if($Submit and $Submit=='Add'){
if($file1!=""){
if($file1_type=="image/jpeg"){
if($file1_size<2100000){
$path1 = "d:\\htdocs\\painting\\uploaded\\";//如果写成"/painting/uploaded/"就无法复制
$pic1=time().".jpg";
$desc1=$path1.$pic1;
echo "file:".$file1."<br>";
echo "desc1".$desc1."<br>";
@copy ($file1,$desc1);
$pic=$desc1;
$pdate=date("Y-m-d H:i:s");
$size1=GetImageSize($file1);
if($size1[0]>$size1[1]) $goutu="h";
if($size1[0]=$size1[1]) $goutu="s";
if($size1[0]<$size1[1]) $goutu="w";
$sql="insert into picture (name,category,caption,pic,pdate,sizeh,sizew,goutu) values ('','','','$pic','$pdate','','','$goutu')";
$q->query($DB,$sql);
$ssql="select max(id) as maxid from picture";
$q->query($DB,$ssql);
$rhw=$q->getrow();
$id=$rhw[maxid];
echo "<p>".$id."<p>";
}
else{
echo "图片尺寸超过2M,请缩小图片后再次上传";
exit;
}
}
else{
echo "只能上传JPG格式的图片";
exit;
}
}
else{
echo "没有上传任何图片";
exit;
}
}if($submit and $submit=='Revise'){}?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head><body bgcolor=ECECFF>
<script language="JavaScript">
function checkform()
{  if(document.uploadedit1.pdate.value=="")
  {
    alert("You must input upload time!");
    document.uploadedit1.pdate.focus();
    return false;
  }
  if(document.uploadedit1.name.value=="")
  {
    alert("You must input name time!");
    document.uploadedit1.name.focus();
    return false;
  }
  if(document.uploadedit1.caption.value=="")
  {
    alert("You must input caption time!");
    document.uploadedit1.caption.focus();
    return false;
  }
  if(document.uploadedit1.sizeh.value=="")
  {
    alert("You must input sizeh!");
    document.uploadedit1.sizeh.focus();
    return false;
  }
  if(document.uploadedit1.sizew.value=="")
  {
    alert("You must input sizew!");
    document.uploadedit1.sizew.focus();
    return false;
  }
    if(document.uploadedit1.category.options[document.uploadedit1.category.selectedIndex].value=="")
  {
    alert("You must select the category!");
    document.uploadedit1.category.focus();
    return false;
  }
}
</script>
<?
if(!isset($id)){
echo "进入路径出现错误,请返回重新提交图片。";
echo "</body></html>";
exit;
}
else{
$sQl="select * from picture where id=$id";
$q->query($DB,$sQl);
$row=$q->getrow();
?>
<table width="500" border="1" cellspacing="0" cellpadding="0">
<form name=uploadedit1.shtm method=post action=uploadedit1.shtm?id=<? echo $id;?> onSubmit="return checkform()">
  <tr>
    <td colspan="2" align=center><img src=<? echo $pic;?> width=450></td> //如果按照上面的windows的方式,图片就无法显示
  </tr>
  <tr>
    <td width="201">提交时间</td>
    <td width="293"><input type="text" name="pdate" value="<? echo $row[pdate];?>"></td>
  </tr>
  <tr>
    <td width="201">作品名</td>
    <td width="293"><input type="text" name="name" value="<? echo $row[name];?>"></td>
  </tr>
  <tr>
    <td>作品分类</td>
    <td>
      <select name="category">
      <option value=>请选择所属类别</option>
    <?
    $ssqql="select id,name from category order by id desc";
    $q->query($DB,$ssqql);
    while($rew=$q->getrow()){
    echo "<option value=".$rew[id]." ";
    if($row[category]=$rew[id]) echo "selected";
    echo ">".$rew[name]."</option>";
    }
    ?>
      </select></td>
  </tr>
  <tr>
    <td>作品描述</td>
    <td><textarea name="caption"><?echo $row[caption];?></textarea></td>
  </tr>
  <tr>
    <td>作品尺寸</td>
    <td>长 
      <input name="sizeh" type="text" size="10" value="<? echo $row[sizeh];?>"> 
    宽 
    <input name="sizew" type="text" size="10" value="<? echo $row[sizew];?>"> 
    (单位:厘米) </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  </form>
</table>
</body>
</html>
<?
}
?>

解决方案 »

  1.   

    可以把保存路径和显示路径保存在不同的变量里面吧copy函数的参数,是要一个相对于文件系统的路径,而不是相对于webserver的路径的
      

  2.   

    都用相对路径,从数据库取出来的时候根据你php所在的位置做点调整就可以了.
    有用到文件路径的话,配合$_SERVER["DOCUMENT_ROOT"]改下就可以了