存储图像到php文件夹没有单独的上传按钮 [英] storing images to folder in php without seperate upload button
问题描述
我是PHP的新手,如果我很傻,请原谅我。
我有一个表单,它在提交时更新表单中的更改。
现在我添加了一个以字段类型作为文件的图片上传字段。我的问题是我不希望它通过点击上传按钮分开上传,而是我想使用我用于表单更新的旧提交按钮。我尝试了几种方法,但失败了。我也只想保存在名称 admin
中,它应该只允许 .jpg
扩展名
我的表单页面代码如下
<?php
if(isset($ _ POST ['update'])){
$ info = pathinfo($ _ FILES ['imagefile'] ['name']);
$ ext = $ info ['extension']; //获取文件的扩展名
$ newname =admin。。$ ext;
$ target = WEB_URL .'img /'.$ newname;
move_uploaded_file($ _FILES ['imagefile'] ['tmp_name'],$ target);
$ name_a = $ _POST ['name'];
$ email_a = $ _POST ['email'];
$ pass_a = $ _POST ['password'];
$ sql =UPDATE admin SET a_name ='$ name_a',a_email ='$ email_a',password ='$ pass_a'其中aid ='$ update_id';
$ retval = mysql_query($ sql,$ link);
if(!$ retval){
die('Could not update data:'。mysql_error());
}
// echo成功更新数据\;
$ b $ result = mysql_query(SELECT * FROM admin where aid ='$ update_id',$ link);
while($ row = mysql_fetch_array($ result)){
$ name = $ row ['a_name'];
$ email = $ row ['a_email'];
$ password = $ row ['password'];
}
mysql_close($ link);
?>
< div class =box box-widget widget-user-2>
< div class =widget-user-header bg-yellow>
< div class =widget-user-image>
<?php echo'< img src =''。$ img。'class =img-circlealt =User Image>'; ?>
< / div>
<! - /.widget-user-image - >
< h3 class =widget-user-username><?php echo$ name; ?>< / H3>
< h5 class =widget-user-desc><?php echo$ role; ?>< / H5>
< / div>
< div class =box-footer no-padding>
< form role =formmethod =postaction =<?php echo $ _SERVER ['PHP_SELF'];?>>
< div class =box-body>
< div class =form-group>
< label for =exampleInputName1>名称< / label>
< input type =textclass =form-controlid =exampleInputName1name =namevalue =<?php echo $ name;?>>
< / div>
< div class =form-group>
< label for =exampleInputEmail1>电子邮件地址< / label>
< input type =emailclass =form-controlid =exampleInputEmail1name =emailvalue =<?php echo $ email;?>>
< / div>
< div class =form-group>
< label for =exampleInputPassword1>密码< / label>
< input type =passwordclass =form-controlid =exampleInputPassword1name =passwordvalue =<?php echo $ password;?>>
< / div>
< div class =form-group>
< label for =exampleInputFile>个人资料图片< / label>< br />
< img id =previewsrc =<?php echo $ img;?> /><峰; br /><峰; br />
< input type =fileonchange =readURL(this); id =exampleInputFilename =imagefile>
< / div>
< / div>
<! - /.box-body - >
< div class =box-footer>
< button type =submitname =updateid =updateclass =btn btn-primary>提交< / button>
< / div>
< / form>
< / div>
< / div>
<! - /.widget-user - >
我尝试过但未能达到要求的行如下所示。这也在上面的代码中。
$ info = pathinfo($ _ FILES ['imagefile'] ['name']);
$ ext = $ info ['extension']; //获取文件的扩展名
$ newname =admin。。$ ext;
$ target = WEB_URL .'img /'.$ newname;
move_uploaded_file($ _FILES ['imagefile'] ['tmp_name'],$ target);
您忘记写 enctype =multipart / form-data
放入您的< form>
中,无法上传任何图片。
你应该改变:
< form role =formmethod =post action =<?php echo $ _SERVER ['PHP_SELF'];?> ENCTYPE = 多部分/格式数据 >
i am newbie in php kindly pardon me if i am silly .
I have a form which on submit updates the changes in the form .
Now i added a image upload field with field type as file . my question is i don't want it to be uploaded by clicking upload button separately, instead i want to use the same old submit button which i use for form update . i tried several ways but failed . also i want to save only in the name admin
and it should allow only .jpg
extension
My form page code is as follows
<?php
if(isset($_POST['update'])) {
$info = pathinfo($_FILES['imagefile']['name']);
$ext = $info['extension']; // get the extension of the file
$newname = "admin.".$ext;
$target = WEB_URL .'img/'.$newname;
move_uploaded_file( $_FILES['imagefile']['tmp_name'], $target);
$name_a = $_POST['name'];
$email_a = $_POST['email'];
$pass_a = $_POST['password'];
$sql = "UPDATE admin SET a_name = '$name_a', a_email = '$email_a', password = '$pass_a' where aid='$update_id' ";
$retval = mysql_query($sql,$link);
if(! $retval ) {
die('Could not update data: ' . mysql_error());
}
// echo "Updated data successfully\n";
}
$result = mysql_query("SELECT * FROM admin where aid='$update_id' ",$link);
while($row = mysql_fetch_array($result)){
$name = $row['a_name'];
$email = $row['a_email'];
$password = $row['password'];
}
mysql_close($link);
?>
<div class="box box-widget widget-user-2">
<div class="widget-user-header bg-yellow">
<div class="widget-user-image">
<?php echo '<img src="' . $img . '" class="img-circle" alt="User Image">'; ?>
</div>
<!-- /.widget-user-image -->
<h3 class="widget-user-username"><?php echo "$name"; ?></h3>
<h5 class="widget-user-desc"><?php echo "$role"; ?></h5>
</div>
<div class="box-footer no-padding">
<form role="form" method = "post" action = "<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="box-body">
<div class="form-group">
<label for="exampleInputName1">Name</label>
<input type="text" class="form-control" id="exampleInputName1" name="name" value="<?php echo $name; ?>">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" name="email" value="<?php echo $email; ?>">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" name="password" value="<?php echo $password; ?>">
</div>
<div class="form-group">
<label for="exampleInputFile">Profile picture</label><br/>
<img id="preview" src="<?php echo $img; ?>" /><br/><br/>
<input type="file" onchange="readURL(this);" id="exampleInputFile" name="imagefile">
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" name="update" id="update" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
<!-- /.widget-user -->
The lines which i tried and failed to achieve my requirement is as follows . which is also there in the above code .
$info = pathinfo($_FILES['imagefile']['name']);
$ext = $info['extension']; // get the extension of the file
$newname = "admin.".$ext;
$target = WEB_URL .'img/'.$newname;
move_uploaded_file( $_FILES['imagefile']['tmp_name'], $target);
You forgot to write enctype="multipart/form-data"
into your <form>
without this attribute you can not upload any image.
You should change :
<form role="form" method = "post" action = "<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
这篇关于存储图像到php文件夹没有单独的上传按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!