0

I have been trying to find a way to upload an image. Well I can upload an image, but I can't get it to do the following.

Firstly I want it to be restricted to only PNG images then I want it to be renamed to pic.png lastly it will be replacing another file called pic.png in the directory above the uploader script.

I've tried many different scripts all over the internet but couldn't find one that would work. Here is my current code for the uploader.

<?php
    $allowedExts = array("png");
    $extension = end(explode(".", $_FILES["file"]["name"]));
    if ((($_FILES["file"]["type"] == "image/png"))
    && ($_FILES["file"]["size"] < 2000000)
    && in_array($extension, $allowedExts)) {
        if ($_FILES["file"]["error"] > 0) {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        } else {
            echo "Upload: " . $_FILES["file"]["name"] . "<br />";
            echo "Type: " . $_FILES["file"]["type"] . "<br />";
            echo "Size: " . ($_FILES["file"]["size"] / 2000) . " Kb<br />";
            echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";\
            if (file_exists("upload/" . $_FILES["file"]["name"])) {
                echo $_FILES["file"]["name"] . " already exists. ";
            } else {
                move_uploaded_file($_FILES["file"]["tmp_name"],
                "../" . $_FILES["file"]["name"]);
                echo "Stored in: " . "../" . $_FILES["file"]["name"];
            }
        }
    } else {
        echo "Invalid file";
    }
?>

Thanks heaps internet!

SteppingHat
  • 1,199
  • 4
  • 19
  • 50
  • Please explain why its not working, errors? by the looks of it its storing it as the file name it started as not "Pic.png" . – BugFinder Oct 15 '12 at 07:23
  • Seems a bit similar to http://stackoverflow.com/questions/701230/how-do-i-rename-a-filename-after-uploading-with-php – ka_lin Oct 15 '12 at 07:24
  • You are asking two questions at once. Divide your problem instead: 1.) [How can I only allow certain filetypes on upload in php?](http://stackoverflow.com/questions/2486329/how-can-i-only-allow-certain-filetypes-on-upload-in-php) and for 2.) it has been pointed another duplicate already to you. – hakre Oct 15 '12 at 07:32
  • I alreayd know how to restrict filetypes. That's already been done in the code above. What I didn't know how to do was to replace another file with the uploaded file. – SteppingHat Oct 15 '12 at 07:34

1 Answers1

0

replace that:

    if (file_exists("upload/" . $_FILES["file"]["name"])) {
        echo $_FILES["file"]["name"] . " already exists. ";
    } else {
        move_uploaded_file($_FILES["file"]["tmp_name"],
        "../" . $_FILES["file"]["name"]);
        echo "Stored in: " . "../" . $_FILES["file"]["name"];
    }

with this:

    move_uploaded_file($_FILES["file"]["tmp_name"],"../pic.png");
Reflective
  • 3,854
  • 1
  • 13
  • 25
  • So simple yet it works beautifully! Thanks heaps! – SteppingHat Oct 15 '12 at 07:35
  • You coud simplify the rest of your code too, but just if you like to have a well looking code. It is good to have a well structured code, but the main thing is to have a workig code :) The other will come with the practice. :) – Reflective Oct 15 '12 at 07:45
  • Thanks! I've been doing PHP for just over a year now. I've kinda got the basics all covered, now I'm up to optimization and the more advanced concepts. Still learning more every day and the language continues to amaze me :) I've simplified it for use in my form such as removing all the echo statements and simplifying the process of checking the requirements of the filetype. So yeah, I'm pretty happy with it myself. Couldn't have gotten it working without you though. Thanks! – SteppingHat Oct 15 '12 at 09:37
  • That is the way ...go ahead :) We all learn some things daily ... nobody is perfect :) – Reflective Oct 15 '12 at 09:58