如何将文件保存在上传文件的相同路径,PHP


How to save file in the same path of uploaded file, PHP?

使用PHP,我的概念是:用户通过提交表单上传图片,然后脚本叠加徽标水印。我想将修改后的图像保存到与原始图像相同的位置(在用户的计算机上)。我现在的代码是这样的:

<form name="form1" method="post" action="watermark.php" enctype="multipart/form-data">
Select file:
<input type="file" name="uploadfile"/>
<input type="submit" value="submit">
</form>

如何知道原始文件的路径以将修改后的图像保存在同一位置?

您不能将其直接保存在用户的计算机上(使用HTML),因为这将违反-everything。相反,您可以在修改后提供文件供下载。如果你使用GD进行水印,代码很简单(但并不完美,应该被视为只是一个线索):

// perform some ops with $img
Header('Content-Type: image/jpeg');
Header('Content-Disposition: attachment; filename=' . $filename);
imagejpeg($img);
imagedestroy($img);

这很简单。我已经做到了。当用户上传图像时,不只是拍摄图像,而是创建一个隐藏字段来存储整个路径。现在,将此路径保留在用户电脑的cookie中,或者将其存储在您的电脑中。返回时,请使用自定义的"另存为"对话框,并在文件之前插入该路径。