Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,457
    Thanks
    15
    Thanked 0 Times in 0 Posts

    upload picture help

    her'es my code

    PHP Code:
    $uploadFile1 $uploadDir $_FILES['photo1']['name'];
    $photo1=$_FILES['photo1']['name'];
    $uploadFile2 $uploadDir $_FILES['photo2']['name'];
    $photo2=$_FILES['photo2']['name']; 
    how do I actually do the upload - and how could i set the variable of photo1 to be null if the user did not upload the picture. (and same with photo2 -- I actually have 6 and they are all optional - -meaning they don't need to be uploaded)

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,769
    Thanks
    19
    Thanked 155 Times in 146 Posts
    http://www.tizag.com/phpT/fileupload.php

    There's no need to set any variables to NULL or whatever you are saying, a file is either downloaded or not....
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #3
    Senior Coder
    Join Date
    May 2004
    Posts
    1,457
    Thanks
    15
    Thanked 0 Times in 0 Posts
    but how do I upload multiple files with Different names

    and i need to get the filename -- will the filename be null automatically if a file was not uploaded?

  • #4
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,769
    Thanks
    19
    Thanked 155 Times in 146 Posts
    here's one way: http://phphybrid.com/tutorials/uploa...with_php/1.php


    the important parts:

    PHP Code:
    <?
    for ($x 1$x <= $num$x++) {

        echo 
    'File '.$x.': <input type="file" name="file'.$x.'" /><br />';

    }
    ?>
    then later:

    PHP Code:
    for ($x 1$x <= $num$x++) {

        
    $file $_FILES['file'.$x];

        if (! 
    is_uploaded_file($file['tmp_name'])) {

            
    $messages[$x-1] = 'File '.$x.': No file selected.';

            continue;

        }

        if (! 
    move_uploaded_file($file['tmp_name'],$dir.$file['name'])) {

            
    $messages[$x-1] = 'File '.$x.': Unable to move file.';

            continue;

        } else {

            
    $messages[$x-1] = 'File '.$x.': Uploaded...';

        }


    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #5
    Senior Coder
    Join Date
    May 2004
    Posts
    1,457
    Thanks
    15
    Thanked 0 Times in 0 Posts
    thanks for your help -- teh follwoing is my code (admin.php?cmd=savenews calls the save news function)

    for some reason this is only returnign a number 1 when submitting the form.
    I need that if the picture is blank it should just leave that variable should be null.

    can you tell me what i'm doing wrong with this code:

    Code:
    function addnews(){
    ?>
    <h1>Add News</h1>
    <form method="post" action="admin.php?cmd=savenews" enctype="multipart/form-data">
    <span class=col1>Title:</span><input type=text name=title><br>
    <span class=col1>Full Article:</span><br>
    <textarea name=fullarticle width=100%></textarea>
    <br>
    <span class=col1>Photo 1:</span>  <input name="photo1" type="file" /><br>
    <span class=col1>Photo 2:</span>  <input name="photo2" type="file" /><br>
    <span class=col1>Photo 3:</span>  <input name="photo3" type="file" /><br>
    <span class=col1>Photo 4:</span>  <input name="photo4" type="file" /><br>
    <span class=col1>Photo 5:</span>  <input name="photo5" type="file" /><br>
    <span class=col1>Photo 6:</span>  <input name="photo6" type="file" /><br>
    <input type=submit name=submit value="Add Article">
    </form>
    <?
    }
    
    function savenews(){
    
    //$uploadDir = 
    $num=6;
    
    
    $dir = '/public_html/pictures/';
    
    $num = $_POST['num'];
    
    $messages = array();
    
    for ($x = 1; $x <= $num; $x++) {
    
        $file = $_FILES['photo'.$x];
    
        if (! is_uploaded_file($file['tmp_name'])) {
    
            $messages[$x-1] = 'File '.$x.': No file selected.';
    
            continue;
    
        }
    
        if (! move_uploaded_file($file['tmp_name'],$dir.$file['name'])) {
    
            $messages[$x-1] = 'File '.$x.': Unable to move file.';
    
            continue;
    
        } else {
    
            $messages[$x-1] = 'File '.$x.': Uploaded...';
    
        }
    
    }
    
    
    
    //foreach ($messages as $msg) {
    
    //    echo $msg.'<br />';
    
    //}
    
    
    
    
    $title=addslashes($_POST['title']);
    $fullarticle=addslashes($_POST['fullarticle']);
    
    	
    $user_query = mysql_query("INSERT INTO news (title, fullarticle,newsdate,photo1,photo2,photo3,photo4) VALUES ('".$title."', '".$fullarticle."', '".date("Y-m-d H:i:s")."','".$photo1."','".$photo2."','".$photo3."','".$photo4."')"); 
    echo $user_query;
    
    
    }

  • #6
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,769
    Thanks
    19
    Thanked 155 Times in 146 Posts
    I think this is right, I didn't test it:

    PHP Code:
    <HTML>
    <HEAD>
    <TITLE></TITLE>
    </HEAD>

    <BODY>


    <?

    // file admin.php


    function addnews()
    {
        
    ?><h1>Add News</h1>
        <form method="post" action="admin.php" enctype="multipart/form-data">
        <span class=col1>Title:</span><input type=text name=title><br>
        <span class=col1>Full Article:</span><br>
        <textarea name=fullarticle width=100%></textarea>
        <br>
        <span class=col1>Photo 1:</span>  <input name="photo1" type="file" /><br>
        <span class=col1>Photo 2:</span>  <input name="photo2" type="file" /><br>
        <span class=col1>Photo 3:</span>  <input name="photo3" type="file" /><br>
        <span class=col1>Photo 4:</span>  <input name="photo4" type="file" /><br>
        <span class=col1>Photo 5:</span>  <input name="photo5" type="file" /><br>
        <span class=col1>Photo 6:</span>  <input name="photo6" type="file" /><br>
        <input type=submit name=submit value="Add Article">
        </form><?
    }

    function 
    savenews()
    {
        
    $dir '/public_html/pictures/';

        for (
    $x 1$x <= 6$x++) 
        {
            
    $file $_FILES['photo'.$x];

            if (!
    is_uploaded_file($file['tmp_name'])) 
            {
                continue;
            }

            if (!
    move_uploaded_file($file['tmp_name'],$dir.$file['name'])) 
            {
                continue;
            } 
        }
        
        
    $title=addslashes($_POST['title']);
        
    $fullarticle=addslashes($_POST['fullarticle']);

        
    $query "INSERT INTO news (title, fullarticle,newsdate,photo1,photo2,photo3,photo4,photo5,photo6) VALUES ('".$title."', '".$fullarticle."', '".date("Y-m-d H:i:s")."','".$_FILES['photo1']['name']."','".$_FILES['photo2']['name']."','".$_FILES['photo3']['name']."','".$_FILES['photo4']['name']."','".$_FILES['photo5']['name']."',,'".$_FILES['photo6']['name']."')";
        
    $result mysql_query($query); 
        if (
    msql_affected_rows($result) < 1)
        {
            die(
    "The table row was not added.");
        }    
    }



    if (
    $_POST['submit'])
    {
        
    savenews();
        echo 
    "The article was added!<br><br>";
        unset(
    $_POST['submit']);
    }

    addnews();


    ?>




    </BODY>
    </HTML>
    Last edited by chump2877; 02-13-2006 at 04:48 PM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #7
    Senior Coder
    Join Date
    May 2004
    Posts
    1,457
    Thanks
    15
    Thanked 0 Times in 0 Posts
    thanks for your help but the names of the files are going in as blank -- do you have any idea why? the pictures are being uploaded though.
    (though would you know how to have it rename the picture if a picture with the same name already exists?)

  • #8
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,769
    Thanks
    19
    Thanked 155 Times in 146 Posts
    the names of the files are going in as blank -- do you have any idea why
    yea, you need to open a connection to your database probably.....


    though would you know how to have it rename the picture if a picture with the same name already exists?
    Use the filesystem functions (http://us2.php.net/manual/en/ref.filesystem.php) to scan your upload directory and fill up an array full of all the file names in the directory....cycle through the array and if $file['name'] already exists, then rename the file you are trying to upload to something else when you move it from the server temp directory..
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!


  •  

    Posting Permissions

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