【问题标题】:How to upload photo with text?如何上传带文字的照片?
【发布时间】:2013-04-15 09:00:32
【问题描述】:

这是学生资料信息表格,当我提交表格时,它工作正常,所有数据都完美保存到 mysql 表中,但我想上传带有信息的照片

当我填写表格并在提交表格后附上图片时

图片目录不上传,但mysql表中的照片名称保存完美,但不上传到图片文件夹。

我该怎么做,请帮我解决这个问题?

提交表格

 <form action="" method="post">
 <div>
   <p><span class="style9"><strong>G.R.N No:</strong></span><strong>  *</strong>
     <input name="grn" type="text" value="<?php echo $grn; ?>" size="50" />
   </p>
   <p><span class="style9"><strong>Name:</strong></span><strong>  *</strong>
     <input name="name" type="text" value="<?php echo $name; ?>" size="50" />
   </p>
   <p><span class="style9"><strong>Home Address:</strong></span><strong>  *</strong>
     <textarea name="address" cols="50"><?php echo $address; ?></textarea>
   </p>
   <p><span class="style9"><strong>Father Name:</strong></span><strong>  *</strong>
     <input name="fathername" type="text" value="<?php echo $fathername; ?>" size="50" />
   </p>
      <p><span class="style9"><strong>Mother Name:</strong></span><strong>  *</strong>
     <input name="mothername" type="text" value="<?php echo $mothername; ?>" size="50" />
    </p>
    <p><span class="style9"><strong>Date Of Birth :</strong></span><strong>  *</strong>
      <input name="age" type="text" value="<?php echo $age; ?>" size="50" />
   </p>
      <p><span class="style9"><strong>Religion :</strong></span><strong>  *</strong>
        <input name="religion" type="text" value="<?php echo $religion; ?>" size="50" />
   </p>
   <p><span class="style9"><strong>Parents Cell No :</strong></span><strong>  *</strong>
     <input name="phoneno" type="text" value="<?php echo $phoneno; ?>"/>
   </p>
   <p><strong>Home Phone No:</strong>
     <input name="phoneno2" type="text" value="<?php echo $phoneno2; ?>" />
   </p>
   <p><span class="style9"><strong>Date Of Join :</strong></span><strong>  *</strong>
     <input id="fullDate" name="dateofjoin" type="text" value="<?php echo $dateofjoin; ?>" size="50" />
   </p>
    <p><span class="style9"><strong>Class :</strong></span><strong>  *</strong>
      <input  name="class" type="text" value="<?php echo $class; ?>" size="50" />
   </p>
    <p><span class="style9"><strong>N.I.C :</strong></span><strong>  *</strong>
      <input name="nic" type="text" value="<?php echo $nic; ?>" size="50" />
   </p>
    <span class="style9"><strong>Branch:</strong></span><strong> *</strong>
    <input name="branch" type="text" value="<?php echo $branch; ?>" size="50">
<span class="style9"><strong>Photo:</strong></span><strong> *</strong>
             <input type="hidden" name="size" value="350000">
            <input type="file" name="photo"> 
    <br/>
   <p class="style1">* required</p>
 <input type="submit" name="submit" value="Submit">
 </div>
 </form> 

php代码

<?php 
 }


 //This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);

//This gets all the other information from the form

$photo=($_FILES['photo']['name']);
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}

 // connect to the database
 include('connect-db.php');



 // check if the form has been submitted. If it has, start to process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 // get form data, making sure it is valid
 $grn = mysql_real_escape_string(htmlspecialchars($_POST['grn']));
 $name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
 $address = mysql_real_escape_string(htmlspecialchars($_POST['address']));
 $branch = mysql_real_escape_string(htmlspecialchars($_POST['branch']));
 $phoneno = mysql_real_escape_string(htmlspecialchars($_POST['phoneno']));
 $phoneno2 = mysql_real_escape_string(htmlspecialchars($_POST['phoneno2']));
 $fathername = mysql_real_escape_string(htmlspecialchars($_POST['fathername']));
 $mothername = mysql_real_escape_string(htmlspecialchars($_POST['mothername']));
 $age = mysql_real_escape_string(htmlspecialchars($_POST['age']));
 $religion = mysql_real_escape_string(htmlspecialchars($_POST['religion']));
 $nic = mysql_real_escape_string(htmlspecialchars($_POST['nic']));
 $class = mysql_real_escape_string(htmlspecialchars($_POST['class']));
 $dateofjoin = mysql_real_escape_string(htmlspecialchars($_POST['dateofjoin']));
 $photo = mysql_real_escape_string(htmlspecialchars($_POST['photo']));

 // check to make sure both fields are entered
 if ($grn == '' || $name == '' || $address == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 // if either field is blank, display the form again
 renderForm($grn, $name, $address, $branch, $phoneno, $phoneno2, $fathername, $mothername, $age, $religion, $nic, $class, $dateofjoin,$error);
 }
 else
 {
 // save the data to the database
  mysql_query("INSERT admission SET grn='$grn', name='$name', address='$address', branch='$branch', phoneno='$phoneno', phoneno2='$phoneno2', fathername='$fathername', mothername='$mothername', age='$age', religion='$religion', nic='$nic', class='$class', dateofjoin='$dateofjoin', photo='$photo'")
 or die(mysql_error()); 
 echo "<center>Admission Complete!</center>";
 // once saved, redirect back to the view page

 }
 }
 else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','','','','','','','','','','','','');
 }




?>

【问题讨论】:

  • 您是否检查了您要上传图片的文件夹权限?
  • 在此处查看完整教程..http://www.w3schools.com/php/php_file_upload.asp 用于上传图片并检查上传文件夹的权限..
  • 这是本地主机,没有权限设置不允许在电脑中
  • 检查你缺少的东西的教程..enctype 是缺少我猜..

标签: php mysql


【解决方案1】:

&lt;form action="" method="post"&gt; 更改为<form action="" method="post" enctype="multipart/form-data">

有关更多信息,请参阅此 w3schools 页面: http://www.w3schools.com/php/php_file_upload.asp

【讨论】:

  • 当我现在在 mysql 表中更改它时不上传图像名称
  • 是的,因为现在您可以将文件用作 $_FILES['photo']['name'] 而不是使用 $_POST
【解决方案2】:

您的表单标签中缺少enctype = multipart/form-data ... 如果您没有设置enctype,则不会填充$_FILES 数组。 所以使用

<form action="" method="post"> to <form action="" method="post" enctype="multipart/form-data">

检查文件夹权限和 php.ini 中的以下设置

file_uploads
upload_max_filesize
max_input_time
memory_limit
max_execution_time
post_max_size

当您使用此功能时,您无法使用 $_POST 访问文件信息,而只能通过 $_FILES 访问文件信息,例如 $_FILES['photo']['name']

修复

替换

$photo = mysql_real_escape_string(htmlspecialchars($_POST['photo']));

$photo = mysql_real_escape_string(htmlspecialchars($_FILES['photo']['name']));

【讨论】:

    猜你喜欢
    • 2017-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    相关资源
    最近更新 更多