0

When uploading images from my website into the designated folder I can see the file in the directory, but cannot open the files nor display them on the webpage.

EDIT This is an issue with my permissions, when trying to open the file in various programs I am receiving permission denied errors.

    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');
?>
  • Do you get any errors in your log? What do you get when you try to load the page, 403, 404, other? – chris85 Nov 27 '15 at 01:00
  • Not receiving any errors, images are showing up in the folder but cannot view the images nor can i open the images on the webpage Array ( [uploadfile] => Array ( [name] => 84108-pooh_bear.jpg [type] => image/jpeg [tmp_name] => C:\Windows\Temp\php5B22.tmp [error] => 0 [size] => 23950 ) ) And I am not receiving any errors from the php, could it be a permissions issue? – Joshua Alcott-Griffin Nov 27 '15 at 01:09
  • Does `move_uploaded_file` move them from the temp directory to your `profile` directory? If you execute the `profile` link does it work or what happens? – chris85 Nov 27 '15 at 01:12
  • yes it moves them to my profile directory , the file appears with the icon of the photo viewing program i am using but when I open the file I get a message from the program saying it cannot open the file – Joshua Alcott-Griffin Nov 27 '15 at 01:15
  • Is the file actually a `.jpg`? – chris85 Nov 27 '15 at 01:18
  • yes I can only upload jpgs – Joshua Alcott-Griffin Nov 27 '15 at 01:19
  • If you download the file from your server to your computer is it openable? – chris85 Nov 27 '15 at 01:28
  • When I click on the image the program says that the format is unreadable – Joshua Alcott-Griffin Nov 27 '15 at 01:46
  • Are the files the same size? – chris85 Nov 27 '15 at 02:04
  • I've uploaded a couple of images, but they've all been under my maximum file size – Joshua Alcott-Griffin Nov 27 '15 at 02:06
  • Does original version = uploaded version? – chris85 Nov 27 '15 at 02:06
  • Just checked, they are both equal size – Joshua Alcott-Griffin Nov 27 '15 at 02:07
  • $user_folder="./profiles/". $user_id; don't think you want the first dot and possibly not the first forward slash either. $user_folder="profiles/". $user_id; and maybe a . '/' after it all $user_folder="profiles/". $user_id .'/'; – Steve Nov 27 '15 at 02:36
  • @Steve I gave that a shot but it still isn't helping the files when uploading, I think this is a permissions issue (just got an access denied error when trying to open it in chrome) – Joshua Alcott-Griffin Nov 27 '15 at 02:50
  • What are the folder's and files' permissions when looked at in Filezilla or similar ftp client? – Steve Nov 27 '15 at 02:52
  • I just tried adding a chmod to the file and same result. When I check the properties on both the directory and file I have full permission – Joshua Alcott-Griffin Nov 27 '15 at 03:05
  • You might have permissions as local admin on your machine but does your server? http://www.sitepoint.com/forums/showthread.php?318761-settings-permissions-on-localhost where they say You just need to give the user the apache service is running under write permissions to the appropriate files. Also http://forums.iis.net/t/1186877.aspx?How+do+I+set+permissions+on+wwwroot+files+http+localhost+running+IIS+ – Steve Nov 27 '15 at 03:08
  • @Steve I am not sure. I am running Apache, how would you change its permissions on Windows10? – Joshua Alcott-Griffin Nov 27 '15 at 03:16
  • Now that is a good question! I don't know. Silly question perhaps, but is your webroot for local host outside of the program files folder? - Nothing will ever write into that in Windows 10 I suspect. – Steve Nov 27 '15 at 03:20
  • if it makes any difference I am directing the images to a folder inside of my C:/ – Joshua Alcott-Griffin Nov 27 '15 at 03:22
  • Is it on the root of C or at least one folder in? Try giving it the full path and not the relative one. – Steve Nov 27 '15 at 03:23
  • Latest file access restrictions on files being opened locally - http://stackoverflow.com/questions/2148584/open-a-direct-file-on-the-hard-drive-from-firefox-file – Steve Nov 27 '15 at 03:35
  • As you probably realised there is an error in my earlier comment - should be chmod 0777chmod("/somedir/somefile", 0777); – Steve Nov 27 '15 at 03:53
  • http://stackoverflow.com/questions/33965692/php-include-doesnt-work-on-files-other-than-index Quirel's comment might point out how to define the full path not just the relative one in case that might be of use. Seems important on localhost. – Steve Nov 28 '15 at 00:39
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/96392/discussion-on-question-by-joshua-alcott-griffin-cannot-interact-with-uploaded-im). – elixenide Nov 28 '15 at 01:04

1 Answers1

0

I think you did not set the right permission to the folder, just try it in this way:

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

If this do not work we would need some more detailed information in order to help you!

Franco
  • 2,309
  • 1
  • 11
  • 18
  • It did not work, in the photoviewer it says that the file is an unknown format (i know that the file I am uploaded are jpgs, and they are saving in the directory as jpgs) – Joshua Alcott-Griffin Nov 27 '15 at 01:46
  • You need to check the mime type of the image you are uploading. – Franco Nov 27 '15 at 01:54
  • Never heard of mime type before, do you mean the file format by chance? I am only uploading jpgs – Joshua Alcott-Griffin Nov 27 '15 at 01:56
  • I understand that but is important to make some checking before you update the images, I will post an other answer to explain that. – Franco Nov 27 '15 at 02:05
  • I see in this line "$full_file_name ="profiles/".$user_id."/".$file_name. ".jpg";" that you hard code the image extension. (.jpg) and in this way you MUST BE SURE the image you are uploading is really an jpg. Anyway your code is a little messy and hard to read. Are you using some framework to manage the upload? – Franco Nov 27 '15 at 02:20
  • I just saw that you mention: " the file appears with the icon of the photo viewing program...", what are you using to show the uploaded images? – Franco Nov 27 '15 at 02:29
  • Yeah sorry if it is a little messy (i'm a college student and this is the first time I've handled uploading photos) I am using Fast Stone Image Viewer.. I think it is a permission issue since I tried opening it with chrome and I got an access denied error – Joshua Alcott-Griffin Nov 27 '15 at 02:49