1

Can someone tell me why my script won't upload image files. Additionally, is there some limitation set on the size of the file?

Basically the way it should work is that every time an image is uploaded the script should change the maximum size, the problem is that when I try to upload any image the if statement seems to fail and it just skips to the else.

The first part is the php:

if (isset($_FILES['image']['name'])) {

$title = sanitizeString($_POST['title']);
$title = nl2br($title);
$saveto = str_replace(" ", "", $title);
$saveto = str_replace("'", "", $saveto);
$saveto = "blogImages/$saveto.jpg";
if (move_uploaded_file($_FILES['image']['tmp_name'], $saveto)) {
    echo "You Have Succesfully Uploaded an Image!";
    $typeok = true;

    switch($_FILES['image']['type'])
        {
            case "image/gif":   $src = imagecreatefromgif($saveto); break;
            case "image/jpeg":  // Both regular and progressive jpegs
            case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break;
            case "image/png":   $src = imagecreatefrompng($saveto); break;
            default:            $typeok = FALSE; break;
        }

    if ($typeok)
            {
                list($w, $h) = getimagesize($saveto);

                $max = 800;
                $tw  = $w;
                $th  = $h;

                if ($w > $h && $max < $w)
                {
                    $th = $max / $w * $h;
                    $tw = $max;
                }
                elseif ($h > $w && $max < $h)
                {
                    $tw = $max / $h * $w;
                    $th = $max;
                }
                elseif ($max < $w)
                {
                    $tw = $th = $max;
                }

                $tmp = imagecreatetruecolor($tw, $th);
                imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
                imageconvolution($tmp, array(array(-1, -1, -1),
                    array(-1, 16, -1), array(-1, -1, -1)), 8, 0);
                imagejpeg($tmp, $saveto);
                imagedestroy($tmp);
                imagedestroy($src);
                }

} else {
    echo "You have not uploaded an Image";
}

This is the HTML:

<body>
<form id = 'blogPostInput' method='post' action = 'inputBlog.php'       enctype='multipart/form-data'>
<h3> Enter or Edit a Blog </h3>
<br/>
Blog Post Title:
<br/>
<input type='text' value='' name='title' maxlength='64'/>
<br/>
Enter an Image or Video URL
<br/>
<input type='text' value='' name='video' maxlength = '128'/>
<br/>
<input type='file' name='image' maxlength='150' />
Enter the Blog Post's Text
<br/>
<textarea name='blogPostContent' cols='100' rows='5'></textarea>
<br/>
<input type='hidden' id='timeStamp' name='timeStamp' maxlength = '64'/>
<br/>
<input type='submit' value= 'Save Blog Post'/>
</form>

</body>
JoshG
  • 15
  • 4
  • What is the max file size set to: see http://stackoverflow.com/questions/2184513/php-change-the-maximum-upload-file-size for details – Paul Bailey Jul 23 '13 at 16:26
  • What happened when you debugged it? – noj Jul 23 '13 at 16:27
  • Do you really intend to use the relative path for where you are writing uploaded files? Does the script user have permissions on this directory? – Mike Brant Jul 23 '13 at 16:32
  • Try to print $_FILES["image"]["error"] to get the error – user4035 Jul 23 '13 at 16:32
  • For one thing, you shouldn't need the file's extension, assuming your form could only accept `.jpg`. As in `$saveto.jpg`, should probably be `$saveto = "blogImages/$saveto";`. The server will take the uploaded file as it was uploaded, extension and all. – Funk Forty Niner Jul 23 '13 at 16:33
  • Plus, your `case` isn't taking into account `.jpg`, as in `case "image/jpeg":`. You should add `case "image/jpg":`. – Funk Forty Niner Jul 23 '13 at 16:34
  • One thing I find strange is that your `moving` your uploaded files before checking it with your `case`. Where did you get your code from? – Funk Forty Niner Jul 23 '13 at 16:37
  • I have added in .jpg and changed the $saveto, Print shows "0", most of the code is coming from Oreilly's Learning PHP, MySQL, JavaScript, and CSS: A Step-by-Step Guide to Creating Dynamic Websites. It still isn't working. I'm gonna try to change the php.ini file. – JoshG Jul 23 '13 at 16:41
  • @user2163570 I take it you're responding to me. Please add the `@` symbol in front of my name when doing so, just so I know. Now, I noticed another thing, `sanitizeString` in `sanitizeString($_POST['title']);`. `sanitizeString` is a function you're trying to access, but doesn't exist anywhere else in your "posted" code, why is that? And give me the URL for it, I want to see this for myself. – Funk Forty Niner Jul 23 '13 at 16:46
  • @user2163570 Try it with a very small file size and if that doesn't work (which I doubt it will), then you know the problem is not with your `php.ini` upload_max_filesize etc. – Funk Forty Niner Jul 23 '13 at 16:50
  • @Fred Here is whats inside sanitizeString: function sanitizeString($var) { $var = strip_tags($var); $var = htmlentities($var); $var = stripslashes($var); return mysql_real_escape_string($var); } – JoshG Jul 23 '13 at 17:09
  • @user2163570 Ok thanks. So your posted code, is not your full code then. What is the URL to the tutorial you followed? – Funk Forty Niner Jul 23 '13 at 17:13
  • @Fred The code is coming from a book. I did not find it online. The code above is the full code pertaining to the image upload. It has worked for me in the past, and I'm trying to figure out why it isn't working now. – JoshG Jul 23 '13 at 17:20
  • @user2163570 Ok. Well if you have a previous copy of your working code, then I suggest you take it one step at a time to troubleshoot the problem. However, 99% of the uploading scripts I've seen, have the `move_uploaded_file` at the end and not at the beginning. – Funk Forty Niner Jul 23 '13 at 17:25
  • 1
    @Fred It turns out it was a permissions thing to the file I was writing to. Thanks Everyone – JoshG Jul 23 '13 at 18:27
  • @user2163570 Oooohh Lordy!! Well hey, that's great. I'm glad you figured it out and for letting us/me know. So many things can be a factor when it comes to uploading, such as `.htaccess` file, paths, etc. Cheers :) – Funk Forty Niner Jul 23 '13 at 18:30
  • @MikeBrant You should make it as an answer. (permissions) – Funk Forty Niner Jul 23 '13 at 18:31
  • @Fred OK Just first troubleshooting thing that looked obvious. Didn't enter as answer originally as I just wanted to verify this was checked. I have entered it as answer below now. – Mike Brant Jul 23 '13 at 18:33
  • @MikeBrant Well, one has to offer different possible reasons as to why something may not work. Turned out that yours was the one. (`+1`) on my part Mike ;-) cheers – Funk Forty Niner Jul 23 '13 at 18:35

1 Answers1

2

You should verify that the relative file path being used points to the correct intended location and also verify the the user under which this script is being run has the appropriate write permissions to the directory.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103