如果表单中没有提供默认图像,如何设置默认图像? [英] How can I set a default image if none is provided from form?

查看:103
本文介绍了如果表单中没有提供默认图像,如何设置默认图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个允许你上传图片的表格(这不是强制性的)。如果用户没有上传图像,我希望它设置为默认图像(default.jpg)。



这就是我所拥有的:



Hi,

I have a form that allows you to upload an image (this is not mandatory). If the user does not upload an image I want it to set to a default image (default.jpg).

This is what I have:

$uploadDir = 'images/'; //Image Upload Folder
			if (isset($_POST['submit'])){
				$fileName = $_FILES['Photo']['name'];
				$tmpName  = $_FILES['Photo']['tmp_name'];
				$fileSize = $_FILES['Photo']['size'];
				$fileType = $_FILES['Photo']['type'];
				$filePath = $uploadDir . $fileName;
				$result = move_uploaded_file($tmpName, $filePath);

				if (!$result) {
					echo' An error occurred';
					exit();
				}
				if(!get_magic_quotes_gpc())
				{
					//addslashes adds a backslash before any malicious characters, for security
					$fileName = addslashes($fileName);
					$filePath = addslashes($filePath);
				};





上面的代码在上传图片时效果很好,我只是不知道如何或在哪里设置它如果没有提供,则为默认值。我试过isset和empty但是我一直从if(!result)部分得到错误。



The code above works fine with uploading an image, I just dont know how or where I can set it to default if none is provided. I have tried isset and empty but I keep getting the error from the if(!result) part.

推荐答案

uploadDir = ' images /'; // 图片上传文件夹
如果(isset(
uploadDir = 'images/'; //Image Upload Folder if (isset(


_POST [' submit'] )){
_POST['submit'])){


fileName =
fileName =


这篇关于如果表单中没有提供默认图像,如何设置默认图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆