PHP File Upload And Document Writing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AutumnsDecay
    New Member
    • Mar 2008
    • 170

    PHP File Upload And Document Writing

    Hey there.

    I have a website that works like the following:

    A Javascript / PHP photo gallery is displayed on the page, cycling through 3 images that are uploaded. These images are coded into a javascript file / php file.

    Everytime I want to add a picture, I'll have to ftp the image to the directory and edit the code to add a new picture.

    What I want to do is this:

    Have an upload field where I can select an image to upload. When I click submit, the following have to take place:

    The image needs to be resized to certain dimensions.
    It needs to be uploaded to the proper directory.
    The code needs to edit itself.

    What I was thinking was having a txt document store all the url info for the script. When the uploaded image is uploaded, php will open the text document and add the correct string based on the image name (example: photo2.jpg).

    Then, when the page is viewed, the php script does an include of the text document, and voila. It's displayed.

    Is this possible to do?
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, AutumnsDecay.

    You might want to consider moving your image list to a database of some kind.

    Check out PHP's SQLite support (http://php.net/sqlite).

    Comment

    • AutumnsDecay
      New Member
      • Mar 2008
      • 170

      #3
      I'm fairly new to SQL and PHP going hand in hand. I've typically only ever used PHP for basic functions like mailing and including.

      Are you able to post an example of what I'd have to do for SQLite? I need this done in a fairly timely manner and I just don't know if I'll be able to pull it off in time.

      Any help is greatly appreciated!

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Okay, for now skip the SQLite then.

        Do you want to be able to remove images as well, or only add?

        If all you need to do is add an image, you can simply:

        [code=php]
        $handle = fopen('/path/to/images.txt', 'a');
        fwrite($handle, 'http://url.of/image.jpg' . PHP_EOL);
        fclose($handle) ;
        [/code]

        And you're done (http://php.net/fopen, http://php.net/fwrite, http://php.net/fclose).

        Things get a little more complicated if you need to be able to remove images as well as add them.

        Comment

        • AutumnsDecay
          New Member
          • Mar 2008
          • 170

          #5
          So when I go to upload a picture, I would simply add that to a PHP script?

          <FORM ACTION="uploadd oc.php" METHOD="POST">

          So if I had that script in a php file called uploaddoc.php, that would take place?

          How would I go about automatically resizing the image once it's uploaded?

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Correct. Once the image has been formatted, you'd add its path to the file using code similar to what I suggested.

            To resize your images, you'll want to use PHP's image functions (http://php.net/gd).

            There is a surprising dearth of good articles on image manipulation in PHP (hm... remind me to add a post to my blog!), but this one's pretty decent:

            Comment

            Working...