Image Upload

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rasta
Forum Newbie
Posts: 11
Joined: Thu Aug 15, 2002 3:50 pm

Image Upload

Post by Rasta »

I'm trying up upload images to a folder on the server.

I'm using the code/tutorial I found on zend.com
http://www.zend.com/zend/spotlight/uploading.php

My problem is that the image gets loaded to the temporary folder, and is given a temporary name, but it doesn't get stored in it's permanent place.

I've tried changing the destination path name several times, but each time I get a message saying that the image wasn't stored.

Here's my code:

Code: Select all

<? 
     
    $uploadpath = '/path/to/temp/folder/on/the/server/www/'; 
    $source = $HTTP_POST_FILES&#1111;'file1']&#1111;'tmp_name']; 
    $dest = '/path/to/server/www/images/'; 

        if ( $dest != '' ) &#123; 

            if ( move_uploaded_file( $source, $dest ) ) &#123; 

                echo 'File successfully stored.<BR>'; 

            &#125; else &#123; 

                echo 'File could not be stored.<BR>'; 

            &#125; 

        &#125;  

    &#125;
Any help would be greatly apprecaited.

Thanks
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post by gite_ashish »

hi,

1. if you are using lattest versions (like php 4.2.0 -OR- php 4.1.0), try with:
$source = $_FILES['file1']['tmp_name'];


and go through this:

http://www.devnetwork.net/forums/viewtopic.php?t=511
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post by gite_ashish »

hi,

If your php configuration allows $HTTP_POST_FILES[], then try:

Code: Select all

&lt;?php
echo '--' . $HTTP_POST_FILES&#1111;'file1']&#1111;'error'] . '--';
?&gt;
(actually, this will work with php 4.2.0 only)
:arrow: http://www.php.net/manual/en/features.f ... errors.php


On the filesystem see also what is there on the path:
$HTTP_POST_FILES['file1']['tmp_name'];
This location should exist.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

here is my file upload form i developed from a tutorial in a book i bought...

Code: Select all

&lt;?
// This next conditional determines weather or not to handle the form, depending upon whether or not $File exists.
if ($File) {
	print ("&lt;i&gt;File name: &lt;b&gt;$File_name&lt;/b&gt;&lt;/i&gt;&lt;P&gt;\n");
	print ("&lt;i&gt;File size: &lt;b&gt;$File_size&lt;/b&gt;&lt;/i&gt;&lt;P&gt;\n");
	if (copy ($File, "uploaded/$File_name")) {
		print ("&lt;b&gt;Your file was successfully uploaded!&lt;/b&gt;&lt;P&gt;\n");
	} else {
		print ("&lt;b&gt;Your file could not be copied.&lt;/b&gt;&lt;P&gt;\n");
	}
	unlink ($File);
}

print ("Upload a file to the server:\n");
print ("&lt;form action="FileUpload.php" method=post enctype="multipart/form-data"&gt;\n");
print ("File &lt;input type=file name="File" size=20&gt;&lt;br&gt;\n");
print ("&lt;input type=submit name="submit" value="Submit"&gt;&lt;/form&gt;\n");
?&gt;
on the line:

Code: Select all

if (copy ($File, "uploaded/$File_name")) {
The "uploaded/" is the directory path, change that to suit your style. ;)

BTW this does everything, not just pictures. If you want to limit your users to pics, it can be easily modifyed into it.

enjoy.
Rasta
Forum Newbie
Posts: 11
Joined: Thu Aug 15, 2002 3:50 pm

Still not getting uploaded

Post by Rasta »

Thanks for all of the responses. I really appreciate them.

All of the suggestions got me past my initial problem, of having the code not recognize that the file was even there. Now, the file seems to exist, (I can get a temp_nam, and set a permanent name) but I'm getting the error that says $dest!='',
or in Oromian's case, that $File doesn't exist.

Ashish, when I implemented the $_POST solution that you recommended, it got me one step further, but the error I get is now:

File not supplied, or file too big. (from the following code).

Code: Select all

if ( $dest != '' ) &#123; 
         if ( move_uploaded_file( $_POST&#1111;$source], $dest ) ) &#123; 
                echo 'File successfully stored.<BR>'; 
            &#125; else &#123; 
                echo 'File could not be stored.<BR>'; 
            &#125; 
        &#125;  
    &#125; else &#123; 
        echo 'File not supplied, or file too big.<BR>'; 
    &#125;
I check the file size settings, and it should be enough to accept the less-than-100k image I'm testing with.

Oromian,
This code is great because it seems very simple! I tried it out, but again, I get the error:

Your file could not be copied.

I don't understand because this should mean that the file isn't being recognized, or is "empty". But the file name is showing up, and the path is correct, as are the size requirements for the file.

Thanks for the help! Any further advice is much appreciated...
:mrgreen:
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post by gite_ashish »

hi,

see what this shows:

Code: Select all

echo '--' . $_FILES&#1111;'file1']&#1111;'error'] . '--';
i think this
if ( move_uploaded_file( $_POST[$source], $dest ) )
should be:
if ( move_uploaded_file( $_FILES[$source], $dest ) )
Rasta
Forum Newbie
Posts: 11
Joined: Thu Aug 15, 2002 3:50 pm

Post by Rasta »

When I put in the

$_FILES, I get the same error:

"File not supplied, or file too big
-- 0 -- "


arrrgggghhhh :x
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

hmmm

I just tested the source again and it's workin on my server.

if you want the entire .php file click here for the zip.

Hope it works!
User avatar
sjunghare
Forum Newbie
Posts: 16
Joined: Tue Sep 03, 2002 6:22 am
Location: Pune

Post by sjunghare »

Here is a code that i used to upload the file :

HTML Page :

Code: Select all

<table border=0 cellspacing=0 cellpadding=0>
    <form name="frm" enctype="multipart/form-data" action="phpupload.php" method
="post">
        <tr><td colspan=2><h4> Send FILE to Server :</h4></td></tr>
        <tr><td colspan=2><hr size = 1></td></tr>
        <tr><td><h5>Upload File :</td>
        <td><INPUT type="file" name="txtFile" width=50 value=""></td></tr>
        <tr><td colspan=2><hr size = 1></td></tr>
        <tr><td colspan=2 align="center">
    	<INPUT type="submit" name="btnSubmit" value="Send File">
    	<INPUT type="reset" name="btnClear" value="Clear"></td></tr>
        <tr><td colspan=2><hr size = 1></td></tr>
</FORM>
</table>
PHP File :

Code: Select all

$uploaded = array (
                        'name'      => $txtFile_name,
                        'type'      => $txtFile_type,
                        'size'      => $txtFile_size,
                        'tmp_name'  => $txtFile
                    );

    if(isset($uploaded))
        &#123;
                $source = $uploaded&#1111;"tmp_name"];
                $dest   = "/path/to/folder/efile/".$uploaded&#1111;"name"];
                copy($source,$dest);
        &#125;
Above Code perfectly work for a FILE Upload

My folder permission is : /efile - drwxrw-rwx

Note : Need to give a write permission to directory where u want to upload the file

regards,
Sachin
User avatar
sjunghare
Forum Newbie
Posts: 16
Joined: Tue Sep 03, 2002 6:22 am
Location: Pune

Post by sjunghare »

And u r code i tried on my server, it work fine:

Following parameters are already set while uploading the FILE :
1. Folder permission : drwxrw-rwx
2. register_globals : On
3. upload_max_filesize : 2M
4. upload_tmp_dir : no value
regards,
:) Sachin
Post Reply