我正在开发一个CMS。
表客户端包含许多字段,其中之一就是图像标题。
当用户上传文件(图像)时,该文件将存储在公共(public)文件夹中。
图像标题字段检索文件的最终名称并将其存储在表中。
问题是当用户想要更新信息时。如果用户不想更改图像,当他单击“更新”时,图像标题字段为空,因此图像的路径变为空,现在他只显示(无图像)。
这就是我一直在尝试做的事情:
HTML 表单:
<p>
<label for="image_caption">Image Caption:</label>
<input name="image_caption" id="image_caption" type="text" class="formbox" size="60" disabled="disabled" value="<?php echo htmlentities($row['image_caption']); ?>" />
</p>
<p>
<label for="image">Insert image:</label>
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
<input type="file" name="image" id="foto" value="<?php echo htmlentities($row['image_caption']); ?>"/>
</p>
<p>
<?php
if($row['image_caption']!= ""){
?>
<img src="../images/clientes/<?php echo $row['image_caption'];?>" width="194" height="145" title="Imagem cliente" class="floatright"/>
<?php
}else{
?>
<img src="../images/sem_imagem.gif" width="194" height="145" title="Imagem não disponível" class="floatright"/><br />
<?php
}
?>
</p>
<p>
<input type="submit" name="update" value="Update" />
<input name="cliente_id" type="hidden" value="<?php echo $row['codigo']; ?>" />
</p>
现在上传 PHP 代码(我刚刚插入了我想向您展示的代码):
// This checks if the file input is empty, and if it is, insert the value from the table that was previously inserted
if(empty($_FILES['image']['name'])){
$image_caption = $row['image_caption'];
}else{
$image = str_replace(' ', '_', $_FILES['image']['name']);
// move the file to the upload folder and rename it
move_uploaded_file($_FILES['image']['tmp_name'],UPLOAD_DIR.$foto);
$image_caption = $image;
}
最佳答案
我仍然不确定我是否理解,但是你不能将原始文件的名称作为隐藏字段传递吗?然后,如果没有上传任何内容,请使用它。
编辑:刚刚看到您的评论,您已经有了原始文件的名称,因此将 PHP 更改为:
if(empty($_FILES['image']['name'])){
$image_caption = $_POST['image_caption'];
}else{
....(etc)
应该可以,不是吗?
当然,如果您打算做类似的事情,那么您应该对发布的变量执行某种清理,以确保没有肮脏的事情......
关于php - 如何检索我上传的文件的路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/847615/