【问题标题】:image upload works on localhost but not online图片上传可以在本地主机上工作,但不能在线
【发布时间】:2018-12-08 13:51:15
【问题描述】:

我知道这个问题已经回答了很多次了,但是我上传图片还是有问题..

图像上传在本地主机上可以正常工作,但在远程服务器(linux)上不起作用

我已经尝试过与此问题相关的解决方案。我还尝试更改 777 和 755 的权限,但仍然无法正常工作。

但还是不行。

感谢您的帮助。

//==================== Insert Section ========================
$tablename='tbl_slider';
if(isset($_REQUEST['submit']) && $_REQUEST['submit']=='Submit')
{
    $heading=$_REQUEST['heading'];
    $content = $_REQUEST['content'];


    $data = array(
            'heading' => $heading,
            'content' => $content

    );


    $lastid=$obj->insert($tablename,$data);
    if($lastid>0)
    {
        $uploadpath='../upload/slider/';
        $img=$obj->ImageUpload('image',$uploadpath,'img-');

        if(!empty($img))
        {
            $data['image']=$img['name'];
            $result=$obj->update($tablename,$data,"id='".$lastid."'");
            @unlink($uploadpath.$previous_pic);
        }


        $msg='<div class="alert alert-success alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <strong>    <i class="icon fa fa-check"></i>Success!</strong>
                Product Added Successfully..!
              </div>';
    }
    else
    {
        $msg='<div class="alert alert-danger alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <strong>    <i class="icon fa fa-check"></i>Warning!</strong>
                An Unexpected Error.
             </div>';
    }
}

//============= Function For Image Uploading ==============
    function ImageUpload($fieldname,$uploadpath,$pfx)
    {
        if(count($_FILES[$fieldname]['name'])>0)
        {
            if (!mkdir($uploadpath))
            {
                mkdir($uploadpath,077,true);
            }

            $targetpath = $uploadpath;
            $ext=$_FILES[$fieldname]['name'];
            $ext=explode(".",$ext);
            if($ext[1]=="jpg" || $ext[1]=="jpeg" || $ext[1]=="png" || $ext[1]=="PNG" || $ext[1]=="JPEG" || $ext[1]=="JPG")
            {
                if($_FILES[$fieldname]['size'] <= 2000000)                  // 2MB
                {
                    $filename=$targetpath.$pfx.time().".".$ext[1];              
                    $name=end(explode('/',$filename));
                    if(move_uploaded_file($_FILES[$fieldname]['tmp_name'],$filename))
                    {   
                        return array('source'=>$filename,'name'=>$name);
                    }
                }
            }
            else
            {
                //echo "Please Choose Image Format";
            }
        }
    }

//=============== Insert Into Datebase==========================        
public function insert($table=null,$array_of_values=array()) 
{
    if ($table===null || empty($array_of_values) || !is_array($array_of_values)) return false;
    $fields=array(); $values=array();
    foreach ($array_of_values as $id => $value) {
        $fields[]=$id;
        if (is_array($value) && !empty($value[0])) $values[]=$value[0];
        else $values[]="'".$value."'";
    }
     $s = "INSERT INTO $table (".implode(',',$fields).') VALUES ('.implode(',',$values).')';
    if ($this->con->query($s)) return $this->con->lastInsertId();       
    return false;   
}

【问题讨论】:

  • 你确定077是你想要的mkdir($uploadpath,077,true);中的权限吗?
  • 当然,先生,它正在本地主机上运行,​​但我也尝试了权限 777 和 755,但结果相同。
  • 这听起来像是权限问题。处理localhost 通常取决于运行http 进程的用户。是否正在创建目录?提前创建目录是否也有同样的问题?

标签: php image codeigniter phpmyadmin image-uploading


【解决方案1】:

您可以按照以下步骤解决问题:

  1. 检查error_log
  2. 检查您的代码是否通过了 if : if($lastid&gt;0)
  3. 检查$_FILES['userfile']['error']
  4. 检查phpinfo()ini设置:upload_max_filesizepost_max_sizemax_input_time

【讨论】:

  • 我检查了所有的东西。现在我可以用图像更新数据,但仍然无法上传。即使在数据库中手动插入,我也可以从网站的管理面板更新图像,但无法从管理面板上传。
【解决方案2】:

存在目录权限问题。

give permission to the upload folder

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    • 2018-03-04
    • 2012-03-26
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多