www.webdeveloper.com
Page 21 of 23 FirstFirst ... 111920212223 LastLast
Results 301 to 315 of 340

Thread: How to: Upload images using PHP

  1. #301
    Join Date
    Dec 2009
    Posts
    1

    I want to keep it simple (file names :-)

    How can I let an uploaded file overwrite an existing file that has the same name?

    I was able to comment out the code in the upload processor file that creates a unique filename for the uploaded file, but I get a server error if that file name already exists.

    I want to keep it really simple so the files can be named ID1.jpg, ID2.jpg etc.

  2. #302
    Join Date
    Jun 2008
    Posts
    64
    Quote Originally Posted by bokeh View Post
    If you want to retain the filename but check there is no collission you could do something like this:
    PHP Code:
    $FieldName 'images';
    $UploadsFolder 'uploads/';
    $output '';
    $MaxAttempts 50;

    foreach(
    $_FILES[$FieldName]['error'] as $k => $v)
    {
        if(
    === $v)
        {
            if(
    file_exists($Filename $UploadsFolder $_FILES[$FieldName]['name'][$k]))
            while(
    $MaxAttempts > ++$v and file_exists(
                
    $Filename=$UploadsFolder.preg_replace('/(?=\.[^.]+$)/','-'.$v,$_FILES[$FieldName]['name'][$k])
            ));
            
    $MaxAttempts $v or die('Please rename ' $_FILES[$FieldName]['name'][$k]);
            if(
    move_uploaded_file($_FILES[$FieldName]['tmp_name'][$k], $Filename))
            {
                
    $output .= "<p><em>{$_FILES[$FieldName]['name'][$k]}</em> saved OK!</p>\n";
            }
        }
    }
    $output or $output '<p>No files were saved</p>';
    echo 
    $output
    Newbie here - where would the above go or replace in the multiple.upload.processor.php?

    Quote Originally Posted by knightman View Post
    fwrite($h, "$now-".$_FILES[$fieldname]['name'][$key]."."\r\n");
    This line is giving me two errors:
    Code:
    Warning: Unexpected character in input: '\' (ASCII=92) state=1 in ... multiple.upload.processor.php on line 92
    and

    Code:
    Parse error: syntax error, unexpected T_STRING in ... multiple.upload.processor.php on line 92
    Awesome script, btw. Regarding thumbnails I am just using phpThumb.

  3. #303
    Join Date
    Dec 2009
    Posts
    1
    Hello, thanks for multiuploader. But I need some help. I want upload into 2 directories.

    So file1 in directory1 and file2 in directory2.. Is it possible? Thanks

  4. #304
    Join Date
    Jan 2010
    Posts
    1
    i am testing the multiple image uploader and i getting the following error:

    HTML Code:
    An error has occured: : ...  The upload form is reloading
    thanks for your help!

  5. #305
    Join Date
    Mar 2010
    Posts
    7

    Permissions error

    Hi guys, ive looked all over and followed some peoples suggestions but i cannot get these scripts to work.

    Every time i attempt to upload images, i get the 'insufficient permission' error.

    Using filezilla i have set the 'uploaded_files' directory to '777'.

    I really have know idea how to solve this as the code clearly works for others.


    Any help whatsoever will be much appreciated.

    Thanks in advance
    Lewis

  6. #306
    Join Date
    Jun 2008
    Posts
    13
    Greetings,

    I don't know if this thread is still supported but I have a question regarding MULTIPLE image uploads.

    I would like people to upload two images (Husband and Wife).
    I have two columns in my MySQL database (husband, wife)

    What code do I used to store both file paths to the images in their proper places in the database?

    Thank you
    Kind regards

  7. #307
    Join Date
    Jun 2008
    Posts
    13
    Nevermind, I just solved the coding problem by myself (YUSS!!)

    Another question:

    If I want these images to expire after a year, is there some way to automatically delete these files out of my images folder?

    I can image after a few years that the folder would just get too huge if nothing got deleted.

  8. #308
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Save the upload date in your DB and every so often delete the ones older than a certain age. Could be set up as a cron job.

  9. #309
    Join Date
    Jun 2008
    Posts
    13
    Quote Originally Posted by bokeh View Post
    Save the upload date in your DB and every so often delete the ones older than a certain age. Could be set up as a cron job.

    Awesome, so I can set up a cron job to delete the image itself in my "images" folder?

    Do you know how to set this up? I know how to delete things out of the database but I don't know how to set up a program to delete actual files in a folder.

  10. #310
    Join Date
    Jan 2010
    Posts
    127
    I've got problems with a pre-existing upload form, its quite a long thing so I posted it elsewhere:

    link here

    If someone could help me I'd really appriciate it, it seems like it should upload fine but it just... doesnt.
    L.U.N.E for anyone and everyone who has an interest in literature

  11. #311
    Join Date
    Jul 2011
    Posts
    6
    I realize the last post on this thread is a year ago but it still comes up high on the list of Google searches for multiple file uploads. I tested the code and it works great(in fact the best I found), but I need to add the names of the uploads to a database. I read through the whole thread and saw the addendum that allows for this but it seems to add each image as a separate record. I want to add up to 8 images to a single record (along with other info). Arrays always give me headaches! I can't figure out where or how to add to the code. I need to add the images to fields image1, image2, image3 etc. Any help greatly appreciated!

  12. #312
    Join Date
    Dec 2004
    Posts
    21
    You need to post some code so you can be helped.

  13. #313
    Join Date
    Jul 2011
    Posts
    6
    OK, this is the form:
    Code:
    <?php
    
    // filename: multiple.upload.form.php
    
    // first let's set some variables
    
    // make a note of the current working directory relative to root.
    $directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
    
    // make a note of the location of the upload handler
    $uploadHandler = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'multiple.upload.processor.php';
    
    // set a max file size for the html upload form
    $max_file_size = 300000; // size in bytes
    
    // now echo the html page
    ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    
    <html lang="en">
    	<head>
    		<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
    	
    		<link rel="stylesheet" type="text/css" href="stylesheet.css">
    <title>Upload form</title>
    </head>
    	
    <body>
    <form id="Upload" action="<?php echo $uploadHandler ?>" enctype="multipart/form-data" method="post">
    <h1>Upload form</h1>
    <p><input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>"></p>
    <p><label for="location">Location:</label>
    <input type="text" name="location">
    </p>
    <p><label for="description">Description:</label>
    <input type="text" name="description">
    </p>
    <p><label for="image1">File to upload:</label>
    <input id="image1" type="file" name="file[]">
    </p>
    <p><label for="image2">File to upload:</label>
    <input id="image2" type="file" name="file[]">
    </p>
    <p><label for="image3">File to upload:</label>
    <input id="image3" type="file" name="file[]">
    </p>
    <p>
    <label for="submit">Press to...</label>
    <input id="submit" type="submit" name="submit" value="Upload us!">
    </p>
    </form>
    	
    </body>
    </html>
    I added the following to the original code on the processor page
    Code:
    $q = "INSERT INTO properties ('location','description','image1','image2','image3') VALUES ('$location','$description',";
    foreach($active_keys as $key)
    { 
    	$q .= "'$uploadFilename',"; 
    }
    $q .= ")";
    echo $q;
    What I get with the echo is
    [QUOTE]INSERT INTO properties ('location','description','image1','image2','image3') VALUES ('Main Street','2 unit townhouse','Array','Array','Array',)[QUOTE]

    So 2 problems:

    1. how do I get the actual name of the file and
    2. how do I get rid of the final comma in the query.

  14. #314
    Join Date
    Jul 2011
    Posts
    6
    I should add that immediately before the code I added is the following
    Code:
    // now let's move the file to its final location and allocate the new filename to it 
    @move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename) 
        or error('receiving directory insuffiecient permission', $uploadForm);
    which is where the $uploadFilename in my query came from.

  15. #315
    Join Date
    Jul 2011
    Posts
    6
    OK I see part of my error now. So I replaced
    $uploadFilename
    with
    $uploadFilename[$key]

    in the query and I get the file name, including the whole path from c:

    If I echo this

    Code:
    foreach($active_keys as $key)
    {
    	echo $_FILES[$fieldname]['name'][$key];
    }
    I get the simple names of the files without the path. Somehow I have to put each file name into a variable like $filename1, $filename2, etc. up to $filename8 and add it to the query. The problem is I don't know how to build an array to do that. Also, the user may not upload 8 files so I'll get an error in my query if there are fewer images. I've been struggling with this for days now, so if anyone can help you'll save me from pulling out my hair
    Last edited by Calagoure; 07-07-2011 at 12:19 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles