我有上传个人资料图片的表单,但我需要添加删除个人资料图片的选项。我总是新手所以请帮忙,这里是代码:数据库连接在顶部添加,上传表单正在工作,数据库中的头像图像路径是(建议/头像)
<input name=newimage type=submit value='Change avatar' class=textbox>
<input name=delimage type=submit value='Remove Avatar' class=textbox>
$getav = mysql_query("SELECT * FROM suggestions WHERE id = '$ida'");
$getavarray = mysql_fetch_array($getav);
$getav = $getavarray['avatar'];
if($getav){$avatar = "avatars/$getav";}else{$avatar = "Untitled-1.png";}
if(isset($_POST['newimage'])){
//die(' ');
//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","1000000");
//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
//This variable is used as a flag. The value is initialized with 0 (meaning no error found)
//and it will be changed to 1 if an errro occures.
//If the error occures the file will not be uploaded.
$errors=0;
//checks if the form has been submitted
//reads the name of the file the user submitted for uploading
$image=$_FILES['image']['name'];
//if it is not empty
if ($image)
{
//get the original name of the file from the clients machine
$filename = stripslashes($_FILES['image']['name']);
//get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
//if it is not a known extension, we will suppose it is an error and will not upload the file,
//otherwise we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
//print error message
die('<font color=white face=verdana size=1>Error with the file type. Only JPEG, PNG, GIF and JPG formats allowed.</font>');
$errors=1;
}
else
{
//get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file
//in which the uploaded file was stored on the server
$size=filesize($_FILES['image']['tmp_name']);
//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*10024)
{
die('<font color=white face=verdana size=1>Image size must be 10000kb or less.</font>');
$errors=1;
}
//we will give an unique name, for example the time in unix time format
$image_name=$ida.'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname="avatars/".$image_name;
//we verify if the image has been uploaded, and print error instead
$ra = mysql_query("SELECT * FROM suggestions WHERE id= '$ida'");
$ras = mysql_fetch_array($ra);
$rasface = $ras['avatar'];
$fileaa = "avatars/".$rasface;
if($rasface){
unlink($fileaa); }
$copied = move_uploaded_file($_FILES['image']['tmp_name'], $newname);
mysql_query("UPDATE suggestions SET avatar = '$image_name' WHERE id = '$ida'");
if (!$copied)
{
die('<font color=white face=verdana size=1>Error please try again.</font>');
$errors=1;
}
else {echo "<font color=white face=verdana size=1>Avatar image changed successfully!</font>";}
}}}
我已经添加了删除按钮:但我不知道如何将其链接到删除头像。
答案 0 :(得分:0)
在网站上放置一个复选框;
<input name="remove" type="checkbox" value="1" />
然后在提交表单时检查是否勾选了复选框;
// Was the checkbox ticked to mark the deleting of the image?
if(isset($_POST['remove']) && $_POST['remove'] == 1)
{
// Do a query to get the image's full path - this is unknown to me,
// but you should be able to code this using your database's fields
unlink($path); // This will delete the file from the server
// Then delete the corresponding row in the database
mysql_query("DELETE FROM suggestions WHERE id = 'YOUR_ID_HERE'");
}