简体   繁体   English

在php中发布上传图片

[英]Issue an uploading image in php

I have create file index.php for uploading image and after uploaded it will be stored in database and also displayed in home page. 我已经创建了用于上传图像的index.php文件,上传后它将存储在数据库中并显示在主页中。 Now it works fine. 现在工作正常。

Now i added more fields like firstname,lastname and other user required fields. 现在,我添加了更多字段,例如名字,姓氏和其他用户必填字段。

Now it displays problem uploading image. 现在,它显示上传图像时出现问题。

May i know, what is the exact way to fix it? 我可以知道,修复它的确切方法是什么?

Any help would be appreciated. 任何帮助,将不胜感激。

Thank you!!. 谢谢!!。

This is index.php: 这是index.php:

<html>
<head>
<title>upload images to database</title>
</head>
<body>
<h1>Register Form</h1>
<form action="index.php" method = "POST" enctype = "multipart/form-data">
Upload Photo:<input type= "file" name= "image"><br />
<input type= "submit" value= "upload">
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//connect to database
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("image") or die(mysql_error());
$file = '';
 $file= $_FILES['image']    ['tmp_name'];
 if(!isset($file))
 {
 echo "please select an image";
 }
 else
 {
$image = mysql_real_escape_string(file_get_contents($_FILES['image']['tmp_name']));
$image_name = mysql_real_escape_string($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size ==FALSE)
{
echo "That's not an image";
}
else
    {
    if(!$insert = mysql_query("INSERT INTO upload VALUES('','$image_name','$image')"))
    {
    echo "Problem uploading image";
    }
    else{

    $lastid = mysql_insert_id();
    echo "Image upload.<p/>Your Image</p><img src=get.php?id=$lastid>";

    }
} 
 }
}
?>
</body>
</html>

This code works fine when upload image, but when i add, These lines of code, i got problem uploading image 上载图片时此代码工作正常,但是当我添加这些代码行时,上载图片时出现问题

First Name: <input type = "text" name= "fname"><br />
    Last Name:<input type = "text" name= "lname"><br />
    Password :<input type = "text" name = "password"><br />
    Retype-password: <input type = "text" name = "rpassword"><br />
    Email:<input type = "text" name = "email"><br />
    Phone Num: <input type = "text" name = "phone"><br />
    Address: <input type ="text" name = "address"><br />

Can anyone help me, still what code need to add? 谁能帮我,仍然需要添加什么代码?

Your question is pretty cryptic and vague, but I am risking some assumptions here 您的问题很神秘且含糊,但是我冒了一些假设

i added more fields like firstname,lastname and other user required fields.

If by this you mean that you added more fields to the database table called upload , then your error is in the SQL below: 如果这意味着您向数据库表中添加了更多字段,称为upload ,那么您的错误就在下面的SQL中:

...mysql_query("INSERT INTO upload VALUES('','$image_name','$image')"))...

Here you have not mentioned the field names, which is fine if you are inserting values into all fields. 在这里,您没有提到字段名称,如果您要在所有字段中插入值,那就很好了。 If you are not inserting values into all table fields, then you need to specify the exact fields for which these values are being inserted. 如果您没有在所有表字段中插入值,则需要指定要为其插入这些值的确切字段。 Something like: 就像是:

    ...mysql_query("INSERT INTO upload (field1,field2,field3)
                    VALUES('','$image_name','$image')"))... 

To see whether, as you suspect, the upload fails because of the newly added html code. 您是否怀疑由于新添加的html代码而导致上传失败。
Remove the database part. 删除数据库部分。
The database section is not necessary and only complicates the troubleshooting. 数据库部分不是必需的,只会使故障排除复杂化。

  • The uploadFile will be deleted when script ends. 脚本结束后, uploadFile将被删除。
    You have to move it to a folder inside your root. 您必须将其移动到根目录下的文件夹中。

bool move_uploaded_file ( string $filename , string $destination )

set to a folder inside your app path 设置到您的应用程序路径内的文件夹

$destination ="images/".$_FILES['image']['name'];   

or set to a absolute folder 或设置为绝对文件夹

$destination ="/home/..../http/pre/rid/25/132596/htdocs/myAppDir/images/"
               .$_FILES['image']['name'];   

look for your path with phpinfo(); phpinfo();寻找您的路径phpinfo();

在此处输入图片说明

$file= $_FILES['image']['tmp_name'];
if ( move_uploaded_file ($file , $destination )) {
   $upOK = TRUE;
 } else {
   $upOK = FALSE;
   echo "File could not be moved : ".$destination;
 }         

phpinfo() phpinfo()函数

在此处输入图片说明

Common Pitfalls 常见陷阱

  • The MAX_FILE_SIZE item cannot specify a file size greater than the file size that has been set in the upload_max_filesize in the php.ini file. MAX_FILE_SIZE项目不能指定大于php.ini文件中upload_max_filesize中设置的文件大小的文件。 The default is 2 megabytes. 默认值为2 MB。

  • If a memory limit is enabled, a larger memory_limit may be needed. 如果启用了内存限制,则可能需要更大的memory_limit。 Make sure you set memory_limit large enough. 确保将memory_limit设置得足够大。

  • If max_execution_time is set too small, script execution may be exceeded by the value. 如果将max_execution_time设置得太小,则该值可能会超出脚本执行。 Make sure you set max_execution_time large enough. 确保将max_execution_time设置得足够大。


test for errors 测试错误

switch ($_FILES['image']['error']) {
    case UPLOAD_ERR_OK:
        break;
    case UPLOAD_ERR_NO_FILE:
        echo "No file sent.";
        throw new RuntimeException('No file sent.');
    case UPLOAD_ERR_INI_SIZE:
    case UPLOAD_ERR_FORM_SIZE:
        echo "Exceeded filesize limit.";
        throw new RuntimeException('Exceeded filesize limit.');
    default:
        echo "Unknown errors";
        throw new RuntimeException('Unknown errors.');
}
// You should also check filesize here.
if ($_FILES['image']['size'] > 1000000) {
    echo "Exceeded filesize limit.";
    throw new RuntimeException('Exceeded filesize limit.');
}

How to set Post Values 如何设置后值

[...]
<?php
  if (isset($_POST["fname"])) {$fname=$_POST["fname"];} else {$fname = "";}
  if (isset($_POST["lname"])) {$lname=$_POST["lname"];} else {$lname = "";}
  // et cetera
?>
<form action="index.php" method = "POST" enctype = "multipart/form-data">
    First Name: <input type = "text" name= "fname" value="<?php echo $fname;?>"><br />
    Last Name : <input type = "text" name= "lname" value="<?php echo $lname;?>"><br />
    // et cetera
[...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM