SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2007
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple file uploader: Code provided

    Hello

    I'm wondering if someone can help me convert my single image uploader to a multiple file uploader.

    Basically it takes the image, renames it and uploads it to a folder on my server, with the filename of the image held in my database:

    PHP Code:
    <?php 
    if (isset($_POST['Submit'])) 

          
    // set the image details and upload the file
          
    $picunique time();
          
    $picture "/home/******/images/$picunique.jpg";
          
    move_uploaded_file ($_FILES['ufile']['tmp_name'][0], $picture);
        
          
    // and insert into the database  
          
    $result mysql_query("Insert into image_test (picture) values('"basename($picture) ."')") or die(mysql_error());
          
    $picture_id=mysql_insert_id();
          
    $error "Sucess - everything uploaded";

    ?>
    <form action="" method="post" enctype="multipart/form-data" name="myform" id="myform">
    <h3>Submit your Pic</h3>
    <?php echo $error;?>
      <fieldset>
        <legend>Please Upload your picture below: </legend> 
      <p> 
        <label for="Main Picture">Upload Image:</label> 
        <input name="ufile[]" type="file" id="ufile[]" />
      </p>
      <input name="Submit" type="submit" value="Submit" />
      <input name="Reset" type="reset" value="Reset" />  
      </fieldset>
    </form>
    </body>
    </html>
    Now, i've started the creation of my multiple uploader:

    Here

    with ?num= being any number, and that creates how many upload fields I get on the page dynamically:

    PHP Code:
    <form action="" method="post" enctype="multipart/form-data" name="myform" id="myform">
    <?php
    $number 
    $_GET['num']; 
    for(
    $i 0$i <= $number$i++)
    {
    echo 
    "
    <p>Upload Image ("
    .$i."):<input name=\"ufile[]\" style=\"width:300px;\" type=\"file\" id=\"ufile[]\" /></p>\n";
    }
    ?>
    </form>
    So i'd like to it so it re-names each image like the single uploader I created and saves the filename row, by row in the database.

    Can anyone kindly advise as i'm not sure how this can be done?

    Thanks

  2. #2
    SitePoint Member
    Join Date
    Mar 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Take a look at the code imageshack uses.

  3. #3
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe this will help you:
    upload.js
    Code JavaScript:
    function add_file(id, i)
    {
    	if (document.getElementById(id + '_' + i).innerHTML.search('uploadinputbutton') == -1)
    	{
    		document.getElementById(id + '_' + i).innerHTML = '<input type="file" class="uploadinputbutton" maxsize="" name="' + id + '[]" onchange="return add_file(\'' + id + '\', ' + (i+1) + ');" /><br /><span id="' + id + '_' + (i+1) + '"><input type="button" value="Add other" onclick="add_file(\'' + id + '\', ' + (i+1) + ');" /><\/span>\n';
    	}
    }
    upload.html
    Code HTML4Strict:
    <head>
    <title>Untitled Document</title>
    <script type="text/javascript" src="upload.js"></script>
    </head>
    <body>
    <form name="form" enctype="multipart/form-data" method="post" action="upload.php">
    <table border="0" cellspacing="0" cellpadding="0">
    	<tr>
    		<td style="vertical-align: top; width: 50%;">
    			<div class="header">Files</div><div style="font-size: 90%;">Files entered here will be transferred to the server.</div><br />
    		</td>
    	</tr>
    	<tr>
    		<td style="vertical-align: top; width: 50%;">
    			<input type="file" class="uploadinputbutton" name="file[]" onchange="add_file('file', 1);" /><br />
    			<span id="file_1"><input type="button" value="Add another" onclick="add_file('file', 1);" /></span><br />
    		</td>
    	</tr>
    </table>
    <input type="submit" name="submit" value="submit" />
    </form>
    </body>
    </html>
    upload.php
    PHP Code:
    <?php
    include ( "im.class.php" );
    header"Content-type: text/html" );
    header"Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
    header"Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT" );
    header"Cache-Control: no-cache, must-revalidate" );
    header"Pragma: no-cache" );
    echo 
    "<?xml version=\"1.0\"?>\n";
    echo 
    "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
        \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
    <html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
    ;
    ?>
    <head>
    <title>Untitled Document</title>
    <script type="text/javascript" src="upload.js"></script>
    </head>
    <body>
    <?php

    if ( isset($_POST['submit']) )
    {
        
    $obj = new processImage"file""user_album""thumbs"12075 );
    }

    ?>
    <a title="album" href="album.php">+ album</a>
    </body>
    </html>
    img.class.php
    PHP Code:
    <?php
    class processImage
    {
        public function 
    createThumbnail$srcFile$destFile$width$quality 75 )
        {
            
    $thumbnail '';
            if ( 
    file_exists($srcFile) && isset($destFile) )
            {
                
    $size getimagesize$srcFile );
                
    $w number_format$width0',''' );
                
    $h number_format( ($size[1] / $size[0]) * $width0',''' );
                
    $thumbnail self::copyImage$srcFile$destFile$w$h$quality );
            }
            
    // return the thumbnail file name on sucess or blank on fail
            
    return basename$thumbnail );
        }
        public function 
    copyImage$srcFile$destFile$w$h$quality 75 )
        {
            
    $tmpSrc pathinfostrtolower($srcFile) );
            
    $tmpDest pathinfostrtolower($destFile) );
            
    $size getimagesize$srcFile );
            if ( 
    $tmpDest['extension'] == "gif" )
            {
                
    $destFile substr_replace$destFile'gif', -);
                
    $dest imagecreatetruecolor$w$h );
            }
            elseif ( 
    $tmpDest['extension'] == "jpg" )
            {
                
    $destFile substr_replace$destFile'jpg', -);
                
    $dest imagecreatetruecolor$w$h );
            }
            elseif ( 
    $tmpDest['extension'] == "jpeg" )
            {
                
    $destFile substr_replace$destFile'jpeg', -);
                
    $dest imagecreatetruecolor$w$h );
            }
            elseif ( 
    $tmpDest['extension'] == "png" )
            {
                
    $dest imagecreatetruecolor$w$h );
            }
            else
            {
                return 
    false;
            }
            switch ( 
    $size[2] )
            {
                case 
    1// GIF
                    
    $src imagecreatefromgif$srcFile );
                    break;
                case 
    2// JPG
                    
    $src imagecreatefromjpeg$srcFile );
                    break;
                case 
    3// PNG
                    
    $src imagecreatefrompng$srcFile );
                    break;
                default:
                    return 
    false;
                    break;
            }
            
    imagecopyresampled$dest$src0000$w$h$size[0], $size[1] );
            switch ( 
    $size[2] )
            {
                case 
    1:
                    
    imagegif$dest$destFile );
                    break;
                case 
    2:
                    
    imagejpeg$dest$destFile$quality );
                    break;
                case 
    3:
                    
    imagepng$dest$destFile );
            }
            return 
    $destFile;
        }
        public function 
    execute$k$path$thumbs$size )
        {
            foreach ( 
    $_FILES[$k]['error'] as $key => $error )
            {
                if ( 
    $error == UPLOAD_ERR_OK )
                {
                    
    $img "$path/" $_FILES[$k]['name'][$key];
                    
    $t 1;
                    while ( 
    file_exists($img) )
                    {
                        
    $img $path "/" $_FILES[$k]['name'][$key];
                        
    $img substr$img0strpos($img".") ) . "-$tstrstr$img"." );
                        
    $t++;
                    }
                    
    $tmp_name $_FILES[$k]['tmp_name'][$key];
                    
    $mf move_uploaded_file$tmp_name$img );
                    
    $x explode"/"$img );
                    
    $y $path "/" $thumbs "/" end$x );
                    
    self::createThumbnail$img$y$size );
                    if ( 
    $mf )
                    {
                        echo 
    $_FILES[$k]['name'][$key] . " uploaded successfully<br />\n";
                    }
                    else
                    {
                        echo 
    $_FILES[$k]['name'][$key] . " upload failed<br />\n";
                    }
                }
            }
        }
    }
    ?>
    my mobile portal
    ghiris.ro


Bookmarks

Posting Permissions

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