【问题标题】:Cannot interact with uploaded images from my website无法与从我的网站上传的图片进行交互
【发布时间】:2016-03-01 03:48:03
【问题描述】:

从我的网站上传图片到指定文件夹时,我可以看到目录中的文件,但无法打开文件,也无法在网页上显示。

编辑这是我的权限问题,当我尝试在各种程序中打开文件时,我收到权限被拒绝错误。

    include('header.php');
$message = "";
$user_id=$_SESSION['user']['user_id'];
$images = getImageCount($user_id);

if(!isset($_SESSION['user']))
{
    $_SESSION['message'] = "You must be logged in to manage your images";
    header("Location:login.php");
}else if($_SESSION['user']['type'] == INCOMPLETE_USER)
{
    $_SESSION['message'] = "You must create a profile to upload images";
    header("Location:create_profile.php");

}else if($_SESSION['user']['type'] == DISABLED_CLIENT)
{
    $_SESSION['message'] = "Your profile has been disabled";
    header("Location:login.php");

}else if($_SERVER['REQUEST_METHOD'] == 'POST')
{


    print_r($_FILES);


    $user_folder="./profiles/". $user_id;
    echo "test";
    $file=$_FILES['uploadfile'];


      //go to the profile table an SELEECT images FROM profiles WHERE user_id =

    if ($images <= MAXIMUM_IMAGES)
    {


        if ($file['error']!=0)
        {
            $_SESSION['message']= "Upload Failed!";
        }

            else if ($_FILES['uploadfile']['type'] != "image/pjpeg" && $_FILES['uploadfile']['type'] != "image/jpeg")
            {
                $message = "Error! image file must be a'". DEFAULT_FILE_TYPE."'";
            }
                else if ($file['size'] > MAX_FILE_SIZE)
                {
                    $message = "Error! File must be smaller than '".MAX_FILE_SIZE."' bytes";
                }

                    else
                    {
                        $directory = "./profiles/".$user_id;
                        echo $directory;
                        //echo $user_folder;
                        if (!is_dir("profiles/".$user_id))
                        {

                            mkdir("profiles/".$user_id, intval( 0777, 8 ), true);
                            echo 2;

                        }

                    $temp_name=$file["tmp_name"];
                    $new_count = $images + 1;
                    $file_name=$user_id."_".$new_count;
                    echo $file_name;
                    $full_file_name ="profiles/".$user_id."/".$file_name. ".jpg";

                    move_uploaded_file($temp_name ,$full_file_name);

                    pg_execute($conn,"update_images",array ($new_count,$_SESSION['user']['user_id']));
                    }
    }
        else 
        {

            $message = "Error! no more than " .MAXIMUM_IMAGES . "picture can be uploaded";
        }

    }
else if (!empty($_POST['submit_changes']))
{
    echo "Fail";
    $images= $_SESSION['profile']['images'];

    }

?>

<form id="uploadform" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <?php echo $message; ?>
    <strong>Select image for upload: </strong>
    <input name="uploadfile" type="file" id="uploadfile" />
    <input type="submit" value="Upload New Image" />
    <img src="profiles/sault/saultl_4.jpg" alt = "Sault"/>
</form>


<?php 
    include('footer.php');
?>

【问题讨论】:

  • 您的日志中是否有任何错误?当您尝试加载页面时,您会得到什么,403、404、其他?
  • 未收到任何错误,图像显示在文件夹中,但无法查看图像,也无法打开网页上的图像 Array ( [uploadfile] => Array ( [name] => 84108- pooh_bear.jpg [type] => image/jpeg [tmp_name] => C:\Windows\Temp\php5B22.tmp [error] => 0 [size] => 23950 ) ) 我没有收到来自 php 的任何错误,会不会是权限问题?
  • move_uploaded_file 是否将它们从临时目录移动到您的profile 目录?如果您执行profile 链接,它会起作用还是会发生什么?
  • 是的,它会将它们移动到我的个人资料目录,该文件出现在我正在使用的照片查看程序的图标上,但是当我打开文件时,我收到来自程序的消息说它无法打开文件
  • 文件实际上是.jpg吗?

标签: php web


【解决方案1】:

我觉得你没有给文件夹设置正确的权限,就这样试试吧:

if (!is_dir("profiles/".$user_id))
{
     mkdir("profiles/".$user_id, intval( 0777, 8 ), true);
}

如果这不起作用,我们需要一些更详细的信息来帮助您!

【讨论】:

  • 它不起作用,在照片查看器中它说文件是未知格式(我知道我上传的文件是jpgs,它们在目录中保存为jpgs)
  • 你需要检查你上传的图片的mime类型。
  • 以前没听说过mime类型,你是说文件格式吗?我只上传jpgs
  • 我明白这一点,但在更新图像之前进行一些检查很重要,我将发布另一个答案来解释这一点。
  • 我在这一行看到 "$full_file_name ="profiles/".$user_id."/".$file_name.".jpg";"您对图像扩展名进行硬编码。 (.jpg) 并且通过这种方式,您必须确保您上传的图像确实是 jpg。无论如何,您的代码有点混乱且难以阅读。您是否使用某些框架来管理上传?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-26
  • 2019-12-23
  • 2011-09-13
  • 2014-01-19
  • 1970-01-01
  • 2018-07-10
相关资源
最近更新 更多