www.webdeveloper.com
Page 8 of 23 FirstFirst ... 67891018 ... LastLast
Results 106 to 120 of 340

Thread: How to: Upload images using PHP

  1. #106
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742

  2. #107
    Join Date
    Apr 2007
    Posts
    1
    Hi and Thanks Bokeh for this script. This script is exactly what i need but i have a problem... but not with the script itself. I want to include 2 thing if possible.
    1- a caption box where the user put his email... and when he submitt the file i receive a mail with his mail address and the file name.
    2- Can i put a checkbox required for the submission? (for a TOS thing)

    Any help is wellcome. =)

    Thanks in advance.


    Bill

  3. #108
    Join Date
    Apr 2007
    Posts
    5
    I am a newbie, sorry guys: I made a beautiful website in WebsiteBuilder on my host Servage.net, found a bug in the program (yes) downloaded VodaHost Website Builder, (same program - no bugs) but of course I cannot upload as I do not have a VodaHost account, and have tried interchanging files with the two, to upload to Servage, to no avail. How do I upload these files. Of course I have reinstalled Servage.net WebsiteBuilder about three times. Still bugs. I am going insane.

  4. #109
    Join Date
    May 2007
    Posts
    2

    small glitch

    Hey, nice work on the form Bokeh. Only problem is that in the multiple uploader, it doesn't check if the file names that you are uploading are the same, after renaming them. Sounds a silly thing to do to try and rename them the same after, however here's my dilema.

    am trying to associate uploaded pictures with a user account, allowing them to upload up to 4 pictures to be placed on a page. Using the following code:

    Code:
    foreach($active_keys as $key){
    		$now = "1";
    		$filename = $_FILES[$fieldname]['name'][$key];
    		$ext = ltrim(strrchr($filename,'.'),'.');
    	    while(file_exists($uploadFilename[$key] = $uploadsDirectory.$_SESSION['username'].'-'.$now.'.'.$ext))
    	    {
    		$now++;
    	    }
    		
    }
    everytime i upload more than one pic, it over writes the first key each time, hence only uploading one picture.
    I think the problem is because it's checking if the file exists in the physical drive, then when it doesn't assigns the key that file name then does the same with the next one, therefore naming all of them the same, uploading each file in turn, overwriting the previous one with the next file that is named exactly the same.

    problem is, i can't use you're method of using the time() function to name them because i need to know what the files are called consitently to be able to reference them properly and put them into my DB at a later date.
    i've been trying to fix this myself for 2 days now, and decided it was about time to speak to the source

    any help would be great.

    ddp

  5. #110
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    If you are using a database just pull the next available ID from the database and use that. It's pretty simple.
    PHP Code:
    $query "INSERT INTO `images` VALUES ()";
    mysql_query($query) or die(mysql_error());
    $ID mysql_insert_id(); 
    That assumes you actually have a primary key of course. Now you have an ID use that as the filename. When it comes to doing your final DB insert do it as an UPDATE query on that ID so you update the row that corresponds to that image.

  6. #111
    Join Date
    May 2007
    Posts
    2
    Thats fine for if i don't need to be able to predict exactly what the image file is going to be named. Problem is, i have a preview page for the person creating the page, this is all working with live data from forms previously submitted. i need to give the user the choice of being able to have a page with pictures or without, edit the page add pictures and preview them again etc, and be able to simply view them upon refresh. the upload form is done in a seperate popup window and uploads the images to a temp directory before anything is stored into the db. the preview page simply needs to check if any files in the temp directory have a file name that corresponds to the username that uploaded them, and then display them. but because it's all working with live information, and i want to give the user the oppertunity to remove the pictures without adding anything to the DB and wasting space on it i don't think the method you suggested will work.

    i suppose the only real way to do it is to upload each picture as a seperate field, instead of an array of keys, and then after one has been uploaded, check against it for the next?
    Last edited by ddp; 05-04-2007 at 08:34 PM.

  7. #112
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Quote Originally Posted by ddp
    i suppose the only real way to do it is to upload each picture as a seperate field, instead of an array of keys, and then after one has been uploaded, check against it for the next?
    No that's not the answer. There are many ways to do what you are asking. One is just to check for duplicate values in the array of names:

    Replace:
    PHP Code:
     while(file_exists($uploadFilename[$key] = $uploadsDirectory.$_SESSION['username'].'-'.$now.'.'.$ext))
    {
        
    $now++;

    with (not tested):
    PHP Code:
    while(file_exists($uploadFilename[$key] = $uploadsDirectory.$_SESSION['username'].'-'.($now++).'.'.$ext
          or 
    count(array_unique($uploadFilename)) !== count($uploadFilename)); 
    Personally though I wouldn't put the images in a temporary directory at all; I would hold them in the session and only store them permanantly when you are doing the DB INSERT (or UPDATE) query.

  8. #113
    Join Date
    Jun 2006
    Posts
    159
    hey bokeh and everyone else,

    I've been using the single image upload for a while now, but now i need to use the multi upload form.
    i've just had a go at setting it up, and it's not working quite right...
    at the mo. it's uploading the image to the server and adding to the data base, but where it should be put the filename, style and comments it's writing Array...
    anyway i'm sure it's obvious to everyone else what i've done wrong so here's my code

    upload form

    PHP Code:
    <form id="Upload" action="http://www.unicyclepics.co.uk/multiple.upload.php" enctype="multipart/form-data" method="post">
            <p><?php $username $_SESSION['username'];
                 if( !
    $_SESSION['username'] )
               { echo(
    "<a href=\"newlogin.html\">LogIn</a>"); }
              else { echo(
    "<span class=\"style1\">User Name: $username</span>"); }  ?><br />
              <?php
    if(isset($_GET['msg'])){
    ?>

    <h3><?php echo($_GET['msg']); ?></h3>

    <?php
    }
    ?><br /></p>
              <div class="row">
                <span class="formlabel">File to upload :</span>
                <span class="forminput"><input size="34" type="file" id="file1" name="file[]"></span>
              </div>
              <div class="row">
                <span class="formlabel">Comment :</span>
                <span class="forminput"><textarea rows="5" cols="18" name="comment[]" value="$comment" class="textarea"></textarea></span>
              </div>
              <div class="row">
                <span class="formlabel">select style :</span>
                <span class="forminput"><select name="style[]"><option value=Street>Street</option><option value=trials selected>Trials</option><option value=muni>Muni</option><option value=freestyle>Freestyle</option><option value=hockey>Hockey</option><option value=distance>Distance</option><option value=mis>Miscellaneous</option></select></span>
              </div>
              <div class="spacer">&nbsp;</div>
              
              <div class="row">
                <span class="formlabel">File to upload :</span>
                <span class="forminput"><input size="34" type="file" id="file2" name="file[]"></span>
              </div>
              <div class="row">
                <span class="formlabel">Comment :</span>
                <span class="forminput"><textarea rows="5" cols="18" name="comment[]" value="$comment" class="textarea"></textarea></span>
              </div>
              <div class="row">
                <span class="formlabel">select style :</span>
                <span class="forminput"><select name="style[]"><option value=Street>Street</option><option value=trials selected>Trials</option><option value=muni>Muni</option><option value=freestyle>Freestyle</option><option value=hockey>Hockey</option><option value=distance>Distance</option><option value=mis>Miscellaneous</option></select></span>
              </div>
              <div class="spacer">&nbsp;</div>
              
              <div class="row">
                <span class="formlabel">File to upload :</span>
                <span class="forminput"><input size="34" type="file" id="file3"  name="file[]"></span>
              </div>
              <div class="row">
                <span class="formlabel">Comment :</span>
                <span class="forminput"><textarea rows="5" cols="18" name="comment[]" value="$comment" class="textarea"></textarea></span>
              </div>
              <div class="row">
                <span class="formlabel">select style :</span>
                <span class="forminput"><select name="style[]"><option value=Street>Street</option><option value=trials selected>Trials</option><option value=muni>Muni</option><option value=freestyle>Freestyle</option><option value=hockey>Hockey</option><option value=distance>Distance</option><option value=mis>Miscellaneous</option></select></span>
              </div>
              <div class="spacer">&nbsp;</div>
              
              <div class="row">
                <span class="formlabel">File to upload :</span>
                <span class="forminput"><input size="34" type="file" id="file4"  name="file[]"></span>
              </div>
              <div class="row">
                <span class="formlabel">Comment :</span>
                <span class="forminput"><textarea rows="5" cols="18" name="comment[]" value="$comment" class="textarea"></textarea></span>
              </div>
              <div class="row">
                <span class="formlabel">select style :</span>
                <span class="forminput"><select name="style[]"><option value=Street>Street</option><option value=trials selected>Trials</option><option value=muni>Muni</option><option value=freestyle>Freestyle</option><option value=hockey>Hockey</option><option value=distance>Distance</option><option value=mis>Miscellaneous</option></select></span>
              </div>
              <div class="spacer">&nbsp;</div>
              <div id="fileDiv_5" style="display:none">
              <div class="row">
                <span class="formlabel">File to upload :</span>
                <span class="forminput"><input size="34" type="file" id="file5"  name="file[]"></span>
              </div>
              <div class="row">
                <span class="formlabel">Comment :</span>
                <span class="forminput"><textarea rows="5" cols="18" name="comment[]" value="$comment" class="textarea"></textarea></span>
              </div>
              <div class="row">
                <span class="formlabel">select style :</span>
                <span class="forminput"><select name="style[]"><option value=Street>Street</option><option value=trials selected>Trials</option><option value=muni>Muni</option><option value=freestyle>Freestyle</option><option value=hockey>Hockey</option><option value=distance>Distance</option><option value=mis>Miscellaneous</option></select></span>
              </div>
              </div>
              <div class="spacer">&nbsp;</div>
    continued in next post..
    sorry it's so long..

  9. #114
    Join Date
    Jun 2006
    Posts
    159
    PHP Code:
    <div id="fileDiv_6" style="display:none">
              <
    div class="row">
                <
    span class="formlabel">File to upload :</span>
                <
    span class="forminput"><input size="34" type="file" id="file6"  name="file[]"></span>
              </
    div>
              <
    div class="row">
                <
    span class="formlabel">Comment :</span>
                <
    span class="forminput"><textarea rows="5" cols="18" name="comment[]" value="$commentclass="textarea"></textarea></span>
              </
    div>
              <
    div class="row">
                <
    span class="formlabel">select style :</span>
                <
    span class="forminput"><select name="style[]"><option value=Street>Street</option><option value=trials selected>Trials</option><option value=muni>Muni</option><option value=freestyle>Freestyle</option><option value=hockey>Hockey</option><option value=distance>Distance</option><option value=mis>Miscellaneous</option></select></span>
              </
    div>
              </
    div>
              <
    div class="spacer">&nbsp;</div>
              <
    div id="fileDiv_7" style="display:none">
              <
    div class="row">
                <
    span class="formlabel">File to upload :</span>
                <
    span class="forminput"><input size="34" type="file" id="file7"  name="file[]"></span>
              </
    div>
              <
    div class="row">
                <
    span class="formlabel">Comment :</span>
                <
    span class="forminput"><textarea rows="5" cols="18" name="comment[]" value="$commentclass="textarea"></textarea></span>
              </
    div>
              <
    div class="row">
                <
    span class="formlabel">select style :</span>
                <
    span class="forminput"><select name="style[]"><option value=Street>Street</option><option value=trials selected>Trials</option><option value=muni>Muni</option><option value=freestyle>Freestyle</option><option value=hockey>Hockey</option><option value=distance>Distance</option><option value=mis>Miscellaneous</option></select></span>
              </
    div>
              </
    div>
              <
    div class="spacer">&nbsp;</div>
              <
    div id="fileDiv_8" style="display:none">
              <
    div class="row">
                <
    span class="formlabel">File to upload :</span>
                <
    span class="forminput"><input size="34" type="file" id="file8"  name="file[]"></span>
              </
    div>
              <
    div class="row">
                <
    span class="formlabel">Comment :</span>
                <
    span class="forminput"><textarea rows="5" cols="18" name="comment[]" value="$commentclass="textarea"></textarea></span>
              </
    div>
              <
    div class="row">
                <
    span class="formlabel">select style :</span>
                <
    span class="forminput"><select name="style[]"><option value=Street>Street</option><option value=trials selected>Trials</option><option value=muni>Muni</option><option value=freestyle>Freestyle</option><option value=hockey>Hockey</option><option value=distance>Distance</option><option value=mis>Miscellaneous</option></select></span>
              </
    div>
              </
    div>
              <
    div class="spacer">&nbsp;</div>
              <
    div id="fileDiv_9" style="display:none">
              <
    div class="row">
                <
    span class="formlabel">File to upload :</span>
                <
    span class="forminput"><input size="34" type="file" id="file9"  name="file[]"></span>
              </
    div>
              <
    div class="row">
                <
    span class="formlabel">Comment :</span>
                <
    span class="forminput"><textarea rows="5" cols="18" name="comment[]" value="$commentclass="textarea"></textarea></span>
              </
    div>
              <
    div class="row">
                <
    span class="formlabel">select style :</span>
                <
    span class="forminput"><select name="style[]"><option value=Street>Street</option><option value=trials selected>Trials</option><option value=muni>Muni</option><option value=freestyle>Freestyle</option><option value=hockey>Hockey</option><option value=distance>Distance</option><option value=mis>Miscellaneous</option></select></span>
              </
    div>
              </
    div>
              <
    div class="spacer">&nbsp;</div>
              <
    div id="fileDiv_10" style="display:none">
              <
    div class="row">
                <
    span class="formlabel">File to upload :</span>
                <
    span class="forminput"><input size="34" type="file" id="file10"  name="file[]"></span>
              </
    div>
              <
    div class="row">
                <
    span class="formlabel">Comment :</span>
                <
    span class="forminput"><textarea rows="5" cols="18" name="comment[]" value="$commentclass="textarea"></textarea></span>
              </
    div>
              <
    div class="row">
                <
    span class="formlabel">select style :</span>
                <
    span class="forminput"><select name="style[]"><option value=Street>Street</option><option value=trials selected>Trials</option><option value=muni>Muni</option><option value=freestyle>Freestyle</option><option value=hockey>Hockey</option><option value=distance>Distance</option><option value=mis>Miscellaneous</option></select></span>
              </
    div>
              </
    div>
              <
    div class="spacer">&nbsp;</div>
              
              <
    div class="row">
              <
    script type="text/javascript">
    // <![CDATA[
    document.write('<a id="addOne" href="javascript:addOne()">More..</a>');
    var 
    fileIndex 4;
    function 
    addOne() {
    var 
    link document.getElementById('addOne');
    link.blur();
    document.getElementById('fileDiv_' + ++fileIndex).style.display 'block';
    if (
    fileIndex >= 10) {
    link.style.display 'none';
    }
    }
    // ]]>
    </script>
                <span class="formlabel"></span>
              <span class="forminput"><input id="submit" type="submit" name="submit" value="Upload"></span>
              </div>
            </form> 

  10. #115
    Join Date
    Jun 2006
    Posts
    159
    upload processor

    PHP Code:
    <?php


    session_start
    ();

    $username $_SESSION['username'];
    $comment $_POST['comment'];
    $style $_POST['style'];

    // 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 directory that will recieve the uploaded files
    $uploadsDirectory $_SERVER['DOCUMENT_ROOT'] . $directory_self 'uploaded_files/';

    // make a note of the location of the upload form in case we need it
    $uploadForm 'http://' $_SERVER['HTTP_HOST'] . $directory_self 'Multi upload/multi.images.php';

    // make a note of the location of the success page
    $uploadSuccess 'http://' $_SERVER['HTTP_HOST'] . $directory_self 'Multi upload/multi.images.php?msg=image uploaded, upload another?';

    // name of the fieldname used for the file in the HTML form
    $fieldname 'file';
    //echo'<pre>';print_r($_FILES);exit;



    // Now let's deal with the uploaded files

    // possible PHP upload errors
    $errors = array(=> 'php.ini max file size exceeded'
                    
    => 'html form max file size exceeded'
                    
    => 'file upload was only partial'
                    
    => 'no file was attached');

    // check the upload form was actually submitted else print form
    isset($_POST['submit'])
        or 
    error('the upload form is neaded'$uploadForm);
        
    // check if any files were uploaded and if 
    // so store the active $_FILES array keys
    $active_keys = array();
    foreach(
    $_FILES[$fieldname]['name'] as $key => $filename)
    {
        if(!empty(
    $filename))
        {
            
    $active_keys[] = $key;
        }
    }

    // check at least one file was uploaded
    count($active_keys)
        or 
    error('No files were uploaded'$uploadForm);
            
    // check for standard uploading errors
    foreach($active_keys as $key)
    {
        (
    $_FILES[$fieldname]['error'][$key] == 0)
            or 
    error($_FILES[$fieldname]['tmp_name'][$key].': '.$errors[$_FILES[$fieldname]['error'][$key]], $uploadForm);
    }
        
    // check that the file we are working on really was an HTTP upload
    foreach($active_keys as $key)
    {
        @
    is_uploaded_file($_FILES[$fieldname]['tmp_name'][$key])
            or 
    error($_FILES[$fieldname]['tmp_name'][$key].' not an HTTP upload'$uploadForm);
    }
        
    // validation... since this is an image upload script we 
    // should run a check to make sure the upload is an image
    foreach($active_keys as $key)
    {
        @
    getimagesize($_FILES[$fieldname]['tmp_name'][$key])
            or 
    error($_FILES[$fieldname]['tmp_name'][$key].' not an image'$uploadForm);
    }

    // make a unique filename for the uploaded file and check it is 
    // not taken... if it is keep trying until we find a vacant one
    foreach($active_keys as $key)
    {
        
    $now time();
        while(
    file_exists($uploadFilename[$key] = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name'][$key]))
        {
            
    $now++;
        }
    }

    // now let's move the file to its final and allocate it with the new filename
    foreach($active_keys as $key)
    {
        @
    move_uploaded_file($_FILES[$fieldname]['tmp_name'][$key], $uploadFilename[$key])
            or 
    error('receiving directory insuffiecient permission'$uploadForm);
    }

    //resize if necessary
    foreach($active_keys as $key)
    {
    $max_width  600;
    $max_height 800;
    if((
    $image_spec[0] > $max_width) or ($image_spec[1] > $max_height))
    {
        
    resize($uploadFilename$uploadFilename$max_width$max_height);
    }

    $max_width  175;
    $max_height 140;
    $thumbnailsDirectory $uploadsDirectory.'thumbs/';
    if((
    $image_spec[0] > $max_width) or ($image_spec[1] > $max_height))
    {
        
    resize($uploadFilename, ($thumbFilename str_replace($uploadsDirectory$thumbnailsDirectory$uploadFilename)), $max_width$max_height);
    }
    }
    //in addition to Bokeh's script you can insert each record into a database.
    foreach($active_keys as $key)
    {
    mysql_connect("host","user","pass")
        or die(
    "Could not connect");
    mysql_select_db("db")
        or die(
    "Could not select database");
    $q "INSERT INTO uploaded_images (`user_name`,`filename`,`thumbnail`,`comment`,`date`,`style`) VALUES ('$username','".str_replace($_SERVER['DOCUMENT_ROOT'], ''$uploadFilename)."','".str_replace($_SERVER['DOCUMENT_ROOT'], ''$thumbFilename)."','$comment',NOW(),'$style')";
    mysql_query($q);
    }    
    //if an error
    echo(mysql_error());    
        
    // If you got this far, everything has worked and the file has been successfully saved.
    // We are now going to redirect the client to the success page.
    header('Location: ' $uploadSuccess);

    // make an error handler which will be used if the upload fails
    function error($error$location$seconds 5)
    {
        
    header("Refresh: $seconds; URL=\"$location\"");
        echo 
    '<head>
      <title>error</title>
      <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />

      <!-- **** layout stylesheet **** -->
      <link rel="stylesheet" type="text/css" href="style/style.css" />

      <!-- **** colour scheme stylesheet **** -->
      <link rel="stylesheet" type="text/css" href="style/colour.css" />

    </head>

    <body>
      <div id="main">
        <div id="links">
          <!-- **** INSERT LINKS HERE **** -->
          <a href="http://www.unicyclepics.co.uk/upload/upload.images.php">Unicycle Pictures</a> | <a href="http://www.videos.unicyclepics.co.uk/upload/upload.videos.php">unicycle videos</a> 
        </div>
        <div id="logo"><h1>&nbsp;</h1>
        </div>
        <div id="menu">
          <ul>
              <li><a href="http://www.unicyclepics.co.uk">home</a></li>
              <li><a href="http://www.unicyclepics.co.uk/user.cp.php">user cp</a></li>
              <li><a href="http://www.unicyclepics.co.uk/upload.info.php.php">upload Info </a></li>
              <li><a href="#">n.u.t.s.</a></li>
              <li><a href="#">contact</a></li>
          </ul>
        </div>
        <div id="content">
          <div id="column1">
            <div class="sidebaritem">
              <h1>error</h1>
              <!-- **** INSERT NEWS ITEMS HERE **** -->
            
            </div>
            <div class="sidebaritem">
              <h1>pictures</h1>
              <div class="sbilinks">
                <!-- **** INSERT ADDITIONAL LINKS HERE **** -->
                <ul>
                  <li><a href="http://www.unicyclepics.co.uk/upload/upload.images.php">Upload Picture </a></li>
                  <li><a href="http://www.unicyclepics.co.uk/unicycle.images.php?style=all">Picture Voting</a></li>
                  <li><a href="http://www.unicyclepics.co.uk/top.images.php?style=all">Top Pictures </a></li>
                  <li><a href="#">Archive</a></li>
                </ul>
              </div>
            </div>
            <div class="sidebaritem3">
              <h1>upload info </h1>
              <!-- **** INSERT OTHER INFORMATION HERE **** -->
              <p>
                To upload unicycle pictures, just select the picture using the browse button, add any comments, select style and click upload.<br />
              for more info read <a href="http://www.unicyclepics.co.uk/upload.info.php">upload info</a></p>
            </div>
          </div>
          <div id="column2">
            <h1>error</h1>
            <!-- **** INSERT PAGE CONTENT HERE **** -->
            <p>' 
    $error '</p>
          </div>
        </div>
        <div id="footer">
          copyright &copy; 2007 unicyclepics.co.uk | <a href="http://www.dcarter.co.uk">design by dcarter</a>
        </div>
      </div>
    </body>
    </html>'
    ;
        exit;
    // end error handler

    function resize($source$destination null$w 100$h 100$quality 70)
    {
        
    $details = @getimagesize($source) or die("I cannot open $source");
        
    $type preg_replace('@^.+(?<=/)(.+)$@''$1'$details['mime']);
        
    $source imagecreatefromstring(file_get_contents($source));
        if(
    $details[0] < $details[1])
        {
            
    $w round(($h $details[1]) * $details[0]);
        }
        else
        {
            
    $h round(($w $details[0]) * $details[1]);
        }
        if(
    imageistruecolor($source))
        {
            
    $slate = @imagecreatetruecolor($w$h) or die('Invalid resize dimmensions');
            
    imageAlphaBlending($slatefalse);
            
    imageSaveAlpha($slatetrue);
        }
        else
        {
            
    $slate = @imagecreate($w$h) or die('Invalid resize dimmensions');
            if(
    false !== ($trans = @imagecolorsforindex($sourceimagecolortransparent($source))))
            {
                
    $trans ImageColorAllocate($slate$trans['red'], $trans['green'], $trans['blue']);
                
    imagefilledrectangle($slate00$w 1$h 1$trans);
                
    imagecolortransparent($slate$trans);
            }
        }
        
    imagecopyresampled($slate$source0000$w$h$details[0], $details[1]);
        
    $destination or header('Content-Type: '.$details['mime']);
        eval(
    '@image'.$type.'($slate'.(($type=='jpeg')?',$destination,$quality':($destination?',$destination  ':'')).');');
        
    imagedestroy($source);
        
    imagedestroy($slate);
        
    $destination or die;
    }

    ?>
    Thanks in advance to anyone that can help.

  11. #116
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    In the multiple upload version $uploadFilename etc arrays, so you need to quote the correct key when using the variable: $uploadFilename[$key]

  12. #117
    Join Date
    Jun 2006
    Posts
    159
    Quote Originally Posted by bokeh
    In the multiple upload version $uploadFilename etc arrays, so you need to quote the correct key when using the variable: $uploadFilename[$key]
    Not sure exactly want you mean, could you give me an example and show me where in the script?

    Cheers

    Pete

  13. #118
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Quote Originally Posted by PJStew
    Not sure exactly want you mean, could you give me an example and show me where in the script?

    Cheers

    Pete
    In the single upload script $uploadfilename was a string. In the multiple uploader it needs to hold more than one filename (one for each file), hence it is an array of strings. This means you need to access the variable by key when building your SQL query.

  14. #119
    Join Date
    Jun 2006
    Posts
    159
    Quote Originally Posted by bokeh
    In the single upload script $uploadfilename was a string. In the multiple uploader it needs to hold more than one filename (one for each file), hence it is an array of strings. This means you need to access the variable by key when building your SQL query.
    ok, I understand what you are saying, but i'm not sure exactly how to do that..? do i have to do multiple database submission? i.e.
    PHP Code:
    $q "INSERT INTO uploaded_images (`user_name`,`filename`,`thumbnail`,`comment`,`date`,`style`) VALUES ('$username','".str_replace($_SERVER['DOCUMENT_ROOT'], ''$uploadFilename[1])."','".str_replace($_SERVER['DOCUMENT_ROOT'], ''$thumbFilename[1])."','$comment[1]',NOW(),'$style[1]')";
    mysql_query($q);
    }  

    $q "INSERT INTO uploaded_images (`user_name`,`filename`,`thumbnail`,`comment`,`date`,`style`) VALUES ('$username','".str_replace($_SERVER['DOCUMENT_ROOT'], ''$uploadFilename[2])."','".str_replace($_SERVER['DOCUMENT_ROOT'], ''$thumbFilename[2])."','$comment[2]',NOW(),'$style[2]')";
    mysql_query($q);

    and so on, or is there a quicker/less code way of doing it? or is that not what you mean at all? sorry for being so slow.

    Cheers for you help Bokeh.

    Pete

  15. #120
    Join Date
    May 2007
    Posts
    1

    upload image one place to another place

    //config.php
    <?php
    $host="localhost";
    $uname="root";
    $pass="";
    $database="Business";
    $connection=mysql_connect($host,$uname,$pass)
    or die ("database connection failed!<br>");
    $result=mysql_select_db($database)
    or die ("database could not be selected");
    //echo "db selected";
    ?>

    .....................................................
    uploadimage1.php

    <?php
    session_cache_limiter("pulic");
    session_start();
    if(isset($_POST['submit']))
    {
    $im1 = $_POST['imgpath'];

    $_SESSION['im1']=$_POST['imgpath'];

    $image01=$_FILES['load1']['name'];

    $_SESSION['image01']=$_FILES['load1']['name'];


    upload();

    echo"<p>Thank you for submitting</p>";
    }
    function upload()

    {
    $image01=$_SESSION['image01'];
    $imgpath = $_SESSION['im1'];
    $image_dir = "images";

    copy($imgpath,"$image_dir/$image01");



    include "configimage.php";

    $query="INSERT INTO image(image1) VALUES ('".$image01."')";
    $result =@ mysql_query($query);
    if (!$result)
    {
    die (mysql_error());
    }
    else
    ?>
    <html>
    <head>
    <title></title>

    </head>
    <body >
    <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" name="f2">


    <table border="1" align="center" cellspacing="1" cellpadding="1" width="200">
    <tr>
    <td colspan="2" bgcolor="#dddddd">
    <center><b><h4><?php echo mysql_affected_rows() ?> Record Entered Into The Table</h4></b></center></td>
    </tr>

    <tr bgcolor="#eeeeee">
    <td width="50" bgcolor="#eeeeee">
    <div align="right">IMAGE</div>
    </td>
    <td width="150"><img src="images/<?php echo $image01;?>" height="100" width="150"></td>
    </tr>

    <?php
    }
    ?>
    </table>

    </form>
    </body>
    </html>

    uploadimage.php

    <html>
    <head>
    <title>Images Inserted Into The Databases</title>
    <script language="javascript" type="text/javascript">
    function validate()
    {
    alert("IMAGE/FILE SAVE INTO FOLDER AS WELL AS DATABASE");
    document.f1.imgpath.value=document.f1.load1.value;

    }
    </script>
    </head>

    <body>
    <caption><center><b>IMAGE--UPLOAD</b></center></caption>
    <form name="f1" method="post" action="uploadimage1.php" enctype="multipart/form-data" >


    <table width="50%" border="1" align="center" cellpadding="2" cellspacing="0">
    <tr>
    <td width="250"><b>IMAGE</b></td>
    <td><input type="file" name="load1"></td>
    </tr>
    <tr>

    <input type="hidden" name="imgpath">

    <tr>
    <td align="center">

    <input type="submit" name="submit" value="Submit" onClick="validate();">
    <input type="reset" value="Reset"></td>
    </tr>

    </table>
    </form>
    </body>
    </html>

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