Jump to content

Help with file upload system (PHP Newbie)


Kalam1ty

Recommended Posts

I have been following a tutorial to create my first php application, a file (image) uploading system. I am hoping to add it to my site, http://www.kalam1ty.com/upload.php

 

I have read through the code multiple times and cannot find out what is wrong. I have tried to fix this for about half an hour now and cannot seem to find where the code is wrong. It does not generate any errors and passes all of the checks that I wrote, it just does not upload the image. Nothing in the upload part of the script looks off to me, but I am a PHP newbie, and am not 100% sure.

 

Here is the whole page's code:

 

<?php
        if(isset($_FILES['image'])) {
                $errors = array();
                $allowed_ext = array('jpg', 'jpeg', 'png', 'gif');

                $file_name = $_FILES['image']['name'];
                $file_ext = strtolower(end(explode('.', $file_name )));
                $file_size = $_FILES['image']['size'];
                $file_tmp = $_FILES['image']['tmp'];

                if(in_array($file_ext, $allowed_ext) === false) {
                        $errors[] = 'Extionsion not allowed.';
                }

                if($file_size > 10485760) {
                        $errors[] = 'File too large. Must be under 10MB.';
                }

                if(empty($errors)) {
                //upload file
                if (move_uploaded_file($file_tmp, 'uploads/images'.$file_name)) {
                        echo 'File uploaded to kalam1ty.com/uploads/images/';
                }

                } else {
                        foreach ($errors as $error) {
                                echo $error, '<br>';
                        }
                }
        }
?>

<html>
        <head>
                <title> Kalam1ty - Home </title>
                <link rel="stylesheet" type="text/css" href="style.css" />
                <link href='http://fonts.googleapis.com/css?family=Anonymous+Pro' rel='stylesheet' type='text/css'>
                <link rel="shortcut icon" href="/home/corona/Development/Web/favicon.ico" type="image/x-icon" />
                <meta name="google" value="notranslate">
        </head>

        <body>
                <div id="EncBG">
                        <div id="Navbar">
                                <a href="index.php">Home</a>
                                <a href="downloads.php">Downloads</a>
                                <a href="upload.php">Upload</a>
                                <a href="contact.php">Contact</a>
                        </div>

                        <div id="Enc1">
                                <div id="Content">
                                        <h2>Upload Files</h2>

                                        <form action='' method='POST' enctype='multipart/form-data'>
                                        <p>
                                                <input type='file' name='image'>
                                                <input type='submit' value='Upload'>
                                        </p>
                                        </form>
                                </div>
                        </div>
                        <div id="Footer" class="whitetext" align="center">
                                <b><i>
                                <a href="index.html">HOME</a>
                                //
                                <a href="downloads.html">DOWNLOADS</a>
                                //
                                <a href="#">OTHER</a>
                                </b></i><br>
                                <img src="smlogo.png" alt="Kalam1ty"/>
                        </div>
                </div>
        </body>
</html>

 

Thank you guys very much if you can help me!

Link to comment
Share on other sites

You need to make sure the "directory" you are uploading to has the correct permissions set.  In this case, you need to be able to "write" files to your uploads directory.  So, right click the directory "on the server", click properties, and set the permissions for the "website" user.  Or you could use chmod.

Link to comment
Share on other sites

You should be developing and debugging your code with error_reporting set to E_ALL (or to a -1) and display_errors set to ON so that all the php detected errors will be reported and displayed. Add the following two lines of code immediately after your first opening <?php tag -

ini_set("display_errors", "1");
error_reporting(-1);

 

You should be getting an error concerning the destination folder/file (you are missing a / after 'images' and before the filename.)

Link to comment
Share on other sites

You should be developing and debugging your code with error_reporting set to E_ALL (or to a -1) and display_errors set to ON so that all the php detected errors will be reported and displayed. Add the following two lines of code immediately after your first opening <?php tag -

ini_set("display_errors", "1");
error_reporting(-1);

 

You should be getting an error concerning the destination folder/file (you are missing a / after 'images' and before the filename.)

 

Thanks for your help!

 

Now that that error is solved, I am getting another.

 

Strict Standards: Only variables should be passed by reference in /usr/www/kalam1ty/public/upload.php on line 10 Notice: Undefined index: tmp in /usr/www/kalam1ty/public/upload.php on line 12

 

I'm not sure what the error means, or what undefined index means. Again, help is appreciated.

Link to comment
Share on other sites

The first error is because end expects an actual array variable to reference. You need to assign the result of the explode to a variable, then use that variable in the remainder of the functions.

 

The second error is because the field is - ['tmp_name']

 

Thank you so much! I got it working because of you. The first error can be ignored, but I'm going to try to find out how to fix it. I know you told me how, but I am not very familiar with php, so I'll need to do a little bit of research first.

 

Thanks!!!

Link to comment
Share on other sites

You might be able to ignore the first error, but php won't.

 

Even with error_reporting/display_errors set so that error is not reported/displayed, php must still detect that error and call the error handler routine every time that line of code is executed. The reporting/display/logging of the error is just the last step in the error handler before it returns to the calling code.

Link to comment
Share on other sites

Well, I meant that it would function properly (or so it seemed), but the error would still appear there.

 

I tried to fix the first error, and was unsuccessful.

 

This is the code I ended up with when I tried to fix the error:

<?php
        if(isset($_FILES['image'])) {
                $errors = array();
                $allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
                $file_name = $_FILES['image']['name'];
                $explode_var = array();
                $explode_var[] = '$file_name';
                $file_ext = strtolower(end(explode('.', $explode_var )));
                $file_size = $_FILES['image']['size'];
                $file_tmp = $_FILES['image']['tmp_name'];

                if(in_array($file_ext, $allowed_ext) === false) {
                        $errors[] = 'File type not allowed. Contact kalam1ty@linuxmail.org if you want an extension added.';
                }

                if($file_size > 10485760) {
                        $errors[] = 'File too large. Must be under 10MB.';
                }

                if(empty($errors)) {
                        if (move_uploaded_file($file_tmp, 'uploads/images/'.$file_name)) {
                                echo 'File uploaded to kalam1ty.com/uploads/images/';
                        }
                } else {
                        foreach ($errors as $error) {
                                echo $error, '<br>';
                        }
                }
        }
?>

<html>
        <head>
                <title> Kalam1ty - Home </title>
                <link rel="stylesheet" type="text/css" href="style.css" />
                <link href='http://fonts.googleapis.com/css?family=Anonymous+Pro' rel='stylesheet' type='text/css'>
                <link rel="shortcut icon" href="/home/corona/Development/Web/favicon.ico" type="image/x-icon" />
                <meta name="google" value="notranslate">
        </head>

        <body>
                <div id="EncBG">
                        <div id="Navbar">
                                <a href="index.php">Home</a>
                                <a href="downloads.php">Downloads</a>
                                <a href="upload.php">Upload</a>
                                <a href="contact.php">Contact</a>
                        </div>

                        <div id="Enc1">
                                <div id="Content">
                                        <h2>Upload Images</h2>

                                        <form action='upload.php' method='POST' enctype='multipart/form-data'>
                                        <p>
                                                <input type='file' name='image'>
                                                <input type='submit' value='Upload'>
                                        </p>
                                        </form>
                                </div>
                        </div>
                        <div id="Footer" class="whitetext" align="center">
                                <b><i>
                                <a href="index.html">HOME</a>
                                //
                                <a href="downloads.html">DOWNLOADS</a>
                                //
                                <a href="#">OTHER</a>
                                </b></i><br>
                                <img src="smlogo.png" alt="Kalam1ty"/>
                        </div>
                </div>
        </body>
</html>

 

Do you think you can tell me what I did wrong or lead me on the right path?

Link to comment
Share on other sites

You need to assign the result of the explode to a variable, then use that variable in the remainder of the functions.

 

Original code -

                $file_name = $_FILES['image']['name'];
                $file_ext = strtolower(end(explode('.', $file_name )));

 

Code with stated change -

                $file_name = $_FILES['image']['name'];
                $parts = explode('.', $file_name );
                $file_ext = strtolower(end($parts));

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.