Cek the following codes. It suppose to copy and paste the pictures - to folder photos/ . Yet, I haven't seen the message: "sucess copy file to photos" appears. only, "Data telah tersimpan" or "Successfully saving data to database".

    //Save Picture
    if (isset($_REQUEST['ok'])){

        if (empty($_REQUEST['id']))
            {
            $sqlstr = "INSERT INTO gallery(judul, image) VALUES('".$judul."','".$image."')";

                // copy pictures to photos

                if($_FILES['image']['error'] == 0){
                // success - move uploaded file 

                $uploads_dir = '/photos';   
                $name = $_FILES["pictures"]["name"];
                move_uploaded_file($tmp_name, "$uploads_dir/$name");
                echo "sucess copy file to photos";

                }else{
                // 'there was an error uploading file' stuff here....  
                echo "error uploading file";        
                }   

            }
        else
            $sqlstr = "UPDATE gallery SET judul='".$judul."', image='".$image."' WHERE id=".$_REQUEST['id'];
        $result = mysql_query($sqlstr) or die(mysql_error());

        //Jika mode edit, maka tidak akan dikirimkan konfirmasi kepada subscriber
        //if (empty($_REQUEST['id']))   kirimEmail($idKategori, $judul, $news);
        $confirmation = ($result) ? "Data telah tersimpan." : "Gagal menyimpan data.";  
    }

Recommended Answers

All 26 Replies

     //Save Picture
    if (isset($_REQUEST['ok'])){
    if (empty($_REQUEST['id']))
    {
    $sqlstr = "INSERT INTO gallery(judul, image) VALUES('".$judul."','".$image."')";
    // copy pictures to photos
    if($_FILES['image']['error'] == 0){
    // success - move uploaded file
    $uploads_dir = '/photos';
    $name = $_FILES["pictures"]["name"];
    move_uploaded_file($_FILES['image']['tmp_name'], "$uploads_dir/$name");
    echo "sucess copy file to photos";
    }else{
    // 'there was an error uploading file' stuff here....
    echo "error uploading file";
    }
    }
    else
    $sqlstr = "UPDATE gallery SET judul='".$judul."', image='".$image."' WHERE id=".$_REQUEST['id'];
    $result = mysql_query($sqlstr) or die(mysql_error());
    //Jika mode edit, maka tidak akan dikirimkan konfirmasi kepada subscriber
    //if (empty($_REQUEST['id'])) kirimEmail($idKategori, $judul, $news);
    $confirmation = ($result) ? "Data telah tersimpan." : "Gagal menyimpan data.";
    }

try this i am not tested this you not give the proper path in function

did you change anything? Well, how to give the proper path in function?

  move_uploaded_file($_FILES['image']['tmp_name'], "$uploads_dir/$name");

i change this only

The result is still the same. I basically want all the pictures being uploaded to be moved to directory photos/ is that possible?

I don't see problems (but I am really tired too lol). Make sure that you set your folder permissions for at least chmod 775. I don't remember if move_uploaded_file actually has any default error reporting if it fails.

If that is all good and it still isn't working, I would start throwing in some echos on some of the variables like file name, just to completely make sure they are correct. I would look at it more, but I am about to passout :)

Ahhh.... right when I was about to close the browser I saw your problem davy. You have your missing some brackets, and the procedure is all screwed up. I am surprised you were not seeing errors from that.

<?php
 //Save Picture
    if (isset($_REQUEST['ok']))
    {
        if (empty($_REQUEST['id']))
        {
            $sqlstr = "INSERT INTO gallery(judul, image) VALUES('".$judul."','".$image."')";
                // copy pictures to photos
                if($_FILES['image']['error'] == 0)
                {
                    // success - move uploaded file 
                    $uploads_dir = '/photos';   
                    $name = $_FILES["pictures"]["name"];
                    move_uploaded_file($_FILES['image']['tmp_name'], "$uploads_dir/$name");
                    echo "success copy file to photos";
                }
                else
                {
                    // 'there was an error uploading file' stuff here....  
                    echo "error uploading file";        
                }
        }
        else
        {
            $sqlstr = "UPDATE gallery SET judul='".$judul."', image='".$image."' WHERE id=".$_REQUEST['id'];
            $result = mysql_query($sqlstr) or die(mysql_error());
            //Jika mode edit, maka tidak akan dikirimkan konfirmasi kepada subscriber
            //if (empty($_REQUEST['id']))   kirimEmail($idKategori, $judul, $news);
            $confirmation = ($result) ? "Data telah tersimpan." : "Gagal menyimpan data.";  
        }
    }
    else
    {
        // request was not ok - or something here, or nothing heh
    }
?>

And make sure this line is correct.. is that really supposed to be set to true?

if (empty($_REQUEST['id']))

I change:

if (empty($_REQUEST['id'])) to if (!empty($_REQUEST['id'])) 

for editing mode:

Well, I receive this errors:

Notice: Undefined index: image in C:\xampp\htdocs\RustoleumCustomCMS2\administrator\input_image.php on line 140

Notice: Undefined index: pictures in C:\xampp\htdocs\RustoleumCustomCMS2\administrator\input_image.php on line 144

Notice: Undefined index: image in C:\xampp\htdocs\RustoleumCustomCMS2\administrator\input_image.php on line 145
success copy file to photos

    line: 140: if($_FILES['image']['error'] == 0){
        // success - move uploaded file
        $uploads_dir = '/photos';

    line: 144: $name = $_FILES["pictures"]["name"];
    line: 145: move_uploaded_file($_FILES['image']['tmp_name'], "$uploads_dir/$name");

Post the code to your form that you are using.
Going to sleep now, but I will check it out tomorrow if no one has by then.

input_image.php

<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
            <input type="hidden" name="id" value="<?php echo $id; ?>"/>
            <table>
                <tr>
                    <td>Judul    :</td>
                    <td><input size="30px" type="text" name="judul" value="<?php echo $judul; ?>"/></td>
                </tr>
                <tr>
                    <td>image:</td>
                    <td><input size="30px" type="file" name="image" value="<?php echo $image; ?>"/></td>
                </tr>
                <tr><?php // showing the picture ?>
                    <td>pictures : </id>
                    <td><img src="../photos/<?php echo $image; ?>"/></td>
                </tr>                
                <tr>             
                    <td><input type="submit" name="ok" value="Simpan"/></td>
              </tr>
            </table>
        </form>

I also wonder why my form doesn't save image filename.

$image = store image filename.

 $sqlstr = "INSERT INTO gallery(judul, image) VALUES('".$judul."','".$image."')";

I cannot see where do you pass your $judul and $image variables.

Here is the form where I pass the variable. I have it in the same file.

<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
            <input type="hidden" name="id" value="<?php echo $id; ?>"/>
            <table>
                <tr>
                    <td>Judul    :</td>
                    <td><input size="30px" type="text" name="judul" value="<?php echo $judul; ?>"/></td>
                </tr>
                <tr>
                    <td>image:</td>
                    <td><input size="30px" type="file" name="image" value="<?php echo $image; ?>"/></td>
                </tr>
                <tr><?php // showing the picture ?>
                    <td>pictures : </id>
                    <td><img src="../photos/<?php echo $image; ?>"/></td>
                </tr>                
                <tr>             
                    <td><input type="submit" name="ok" value="Simpan"/></td>
              </tr>
            </table>
        </form>

Well, you had a few different issues going on here. I kept your code as close to original so you could see what I changed around to learn from.

Here is the form:
Nothing really major here except you were missing enctype="multipart/form-data". I also wrapped your picture view in a if statement.

<form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<table>
    <tr>
        <td>Judul    :</td>
        <td><input size="30px" type="text" name="judul" value="<?php echo $judul; ?>"/></td>
    </tr>
    <tr>
        <td>image:</td>
        <td><input size="30px" type="file" name="image" /></td>
    </tr>
    <tr>
        <?php if (isset($_FILES['image'])): ?>
        <td>pictures : </id>
        <td><img src="<?php echo "photos/".$_FILES["image"]["name"]; ?>"/></td>
        <?php endif; ?>   
    </tr>              
    <tr>             
        <td><input type="submit" name="ok" value="Simpan"/></td>
    </tr>
</table>
</form>

Here is your upload code:
When you are dealing with uploaded files and moving things around you want to make sure to use the real path rather than a "web path". That is why I defined SITE_ROOT down below, and so it could just be called and you have the root folder where the script is running. Then you just slap the name of the folder you want to move the files to behind it.

<?php

// define the root for this script
define ('SITE_ROOT', realpath(dirname(__FILE__)));

// uploads directory
$uploads_dir = SITE_ROOT."/photos/";


    if (isset($_REQUEST['ok']))
    {
        if (isset($_FILES['image'])) // check for image
        {
            if (empty($_REQUEST['id']))
            {
                    $sqlstr = "INSERT INTO gallery(judul, image) VALUES('".$judul."','".$image."')";

                    // copy pictures to photos
                    if($_FILES['image']['error'] == 0)
                    {
                        // success - move uploaded file   
                        $name = $_FILES["image"]["name"];
                        move_uploaded_file($_FILES['image']['tmp_name'], $uploads_dir.$name);
                        echo "success copy file to photos";
                    }
                    else
                    {
                        // 'there was an error uploading file' stuff here....  
                        echo "error uploading file";        
                    }
            }
            else
            {
                $sqlstr = "UPDATE gallery SET judul='".$judul."', image='".$image."' WHERE id=".$_REQUEST['id'];
                $result = mysql_query($sqlstr) or die(mysql_error());
                //Jika mode edit, maka tidak akan dikirimkan konfirmasi kepada subscriber
                //if (empty($_REQUEST['id']))   kirimEmail($idKategori, $judul, $news);
                $confirmation = ($result) ? "Data telah tersimpan." : "Gagal menyimpan data.";  
            }
        }
        else
        {
            echo "Unknown error";
        }
    }

?>

Undefined index: $tmp_name in C:\xampp\htdocs\RustoleumCustomCMS2\administrator\input_image.php on line 154
line 154: move_uploaded_file($_FILES['image']['$tmp_name'], "$uploads_dir.$name");

The line should be

 move_uploaded_file($_FILES['image']['tmp_name'], $uploads_dir.$name);

Check the code I gave you again.

ok, it doesn't copy the file to the new location yet. I checked.

Also, in the database I have gallery(id, title, image). Image suppose to be stored with filename, yet, it still empty.

Are you using the exact code I put here? I tested this on my server and it works fine (except for the db stuff, I commented that out). I am assuming you didn't actually just use the code I put up since you had the wrong move_uploaded_file line before.

ok, it doesn't copy the file to the new location yet.

Did it give you an error? Make sure you chmod 775 your photos folder that you are moving images to as well.

Cool. It moves to administrator/photos directory. Yet, instead of administrator/photos is it possible to move it to: ../photos directory?

Also, the filename is not saved in the database.

Are you running the script inside of an Administrators directory?

yes

It is designed to copy to the folder that is in the same directory that you are running from the script from. You can copy it anywhere you want on the server if you get the relative path and the permissions on the folder are set correctly. I am still looking at your code, it is just taking some time because I am multitasking right now.

I figure that one out and the database stuff. One more thing, is it possible to place MAX height or width for the pictures? or is it possible to turn a large picture into thumbnail with the same proportion? The picture that I uploaded turn out way too large. Thanks.

so again, here is the form with a couple of changes. If you echo an variable that you don't have defined yet it throws an error. You can't see it unless you have error reporting set to all but they bother me. So I wrapped them in an if statement.

<form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type="hidden" name="id" value="<?php if(isset($id)){echo $id;} ?>"/>
<table>
    <tr>
        <td>Judul    :</td>
        <td><input size="30px" type="text" name="judul" value="<?php if(isset($judul)){echo $judul;} ?>"/></td>
    </tr>
    <tr>
        <td>image:</td>
        <td><input size="30px" type="file" name="image" /></td>
    </tr>
    <tr>
        <?php if (isset($_FILES['image'])): ?>
        <td>pictures : </id>
        <td><img src="<?php echo "photos/".$_FILES["image"]["name"]; ?>"/></td>
        <?php endif; ?>   
    </tr>              
    <tr>             
        <td><input type="submit" name="ok" value="Simpan"/></td>
    </tr>
</table>
</form>

Here is the upload code. I am a little confused by the logic for the if statement you have checking the id. I guess you are trying to put in some "edit" logic of some sort. I commented it out for testing. You'll notice also that changed the order of operations in some things like the database insert. There really isn't a reason to write information to the database if the upload failed, so I stuck it behind the file copy stuff.

<?php

// define the root for this script
define ('SITE_ROOT', realpath(dirname(__FILE__)));

// uploads directory
$uploads_dir = SITE_ROOT."/photos/";


    if (isset($_REQUEST['ok']))
    {
        if (isset($_FILES['image'])) // check for image
        {
            if (empty($_REQUEST['id']))
            {     
                    // copy pictures to photos
                    if($_FILES['image']['error'] == 0)
                    {

                        $name = $_FILES["image"]["name"];
                        $judul = $_REQUEST['judul'];

                        if(move_uploaded_file($_FILES['image']['tmp_name'], $uploads_dir.$name)){
                            echo "file moved successfully";
                        }
                        else{
                            echo "failed copying file";
                            exit();
                        }

                        // run your insert AFTER the file has been uploaded and copied over
                        $sqlstr = "INSERT INTO gallery (judul, image) VALUES ('$judul','$name')";
                        $result = mysql_query($sqlstr) or die(mysql_error());

                    }
                    else
                    {
                        // 'there was an error uploading file' stuff here....  
                        echo "error uploading file";      
                    }
            }
            else
            {   
                //$sqlstr = "UPDATE gallery SET judul='$judul', image='$image' WHERE id=".$_REQUEST['id'];
                //$result = mysql_query($sqlstr) or die(mysql_error());
                //Jika mode edit, maka tidak akan dikirimkan konfirmasi kepada subscriber
                //if (empty($_REQUEST['id']))   kirimEmail($idKategori, $judul, $news);
                //$confirmation = ($result) ? "Data telah tersimpan." : "Gagal menyimpan data.";  
            }
        }
        else
        {
            echo "Unknown error";
        }
    }

?>

You can generate thumbnails from the images in php but that would be a whole other set of code to add to this. Try taking this one step at a time. Make sure upload and file copy work, then the database, then what ever else. It is easier to clean up small messes one at a time then it is one big large mess.

I think I just start a new discussion thread for that issue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.