php image upload processing

Discussion in 'PHP' started by mdrobiul, Jan 1, 2009.

  1. #1
    I want to upload photo and then show it. I thought like

    file name will be saved as $image and later I'll display it as img src = $image

    but...

    Internet explorer save file name upload to database as C:\Mydocuments\Mypic\picture.jpg

    But firefox save file name upload to database as : picture.jpg

    I've just tried to insert in database as $_POST['image']

    so my script works for firefox..not with IE... should i follow other way for image upload script..
     
    mdrobiul, Jan 1, 2009 IP
  2. GreatMetro

    GreatMetro Peon

    Messages:
    117
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    When you upload a file it goes into the $_FILES array:

    <html>
    <body>

    <form action="" method="post" enctype="multipart/form-data">
    Filename:
    <input type="file" name="file" id="file" />
    <br />
    <input type="submit" name="submit" value="Submit" />
    </form>

    </body>
    </html>

    This will return the following when submitted:


    * $_FILES["file"]["name"] - the name of the uploaded file
    * $_FILES["file"]["type"] - the type of the uploaded file
    * $_FILES["file"]["size"] - the size in bytes of the uploaded file
    * $_FILES["file"]["tmp_name"] - the name of the temporary copy of the file stored on the server
    * $_FILES["file"]["error"] - the error code resulting from the file upload


    Try print_r($_FILES) to see what happens when you upload the file, and check out:

    http://www.w3schools.com/PHP/php_file_upload.asp
     
    GreatMetro, Jan 1, 2009 IP
  3. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #3
    After uploading it, you will need to use the move_uploaded_file() function to have it save the image somewhere on your website since the image is only temporarily stored in the tmp folder and is destroyed after the script runs.
     
    zerxer, Jan 1, 2009 IP
  4. yangyang

    yangyang Banned

    Messages:
    757
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Take a look at this article of a simple PHP File Upload Script, security check and move_uploaded_file are integrated into the Uploaded class.

    For example, you want to upload a photo named 20090112.jpg to the directory 'photos' under the document root of your domain and rename the uploaded file to 'me.jpg', you will have:

    upload.html (the HTML form)
    <form enctype="multipart/form-data" method="post" action="upload.php">
    	<input type="file" name="image" />
    	<button type="submit" name="go">Upload it</button>
    </form>
    HTML:
    upload.php (the php upload handler)
    <?php
    include('Uploaded.php'); // the Uploaded class
    if (isset($_POST['go'])) {
    	$theUploaded = new Uploaded('image');
    	$photoName = $theUploaded -> getFileName('photos/', 'me');
    }
    ?>
    PHP:
    The Uploaded class.

    Therefore after all this, $photoName is the photo file name you can use for later retrieval such as displaying in HTML as <img /> and the photo is ready for access at http://yourdomain.com/photos/me.jpg .
     
    yangyang, Jan 15, 2009 IP
  5. fex

    fex Peon

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Nice upload script. It's just what I needed, but I'm having some difficulties with it..

    My upload.php has got a bit more code because of a text input and a textarea in my form. As it is configured now, I am able to upload photo's. That actually works. The problem is that the rest of my form ($title and $text) are no longer being written to my flat-file database (data.txt). Also when uploading the image, the header is not being executed..

    My upload.php
    <?php
    	require_once('Uploaded.class.php');
    	if(isset($_POST['submit'])){		
    		$title = $_POST['news_title'];
    		$text = $_POST['news_text'];
    		
    		setlocale(LC_ALL, 'nl_NL');
    		$info = "Gepost op " . strftime("%A %e %B %y @ %R");
    		
    		if((isset($title)) && (isset($text))){
    			$att1 = "plugins/news/data.txt"; 
    	        $att2 = fopen ($att1, "r"); 
    	        $currententries = fread ($att2, filesize ($att1)); 
    	        fclose ($att2); 
    	        
    	        $fp = fopen("plugins/news/data.txt", "w+");   
    	        fputs($fp, 	"<div id='news_wrapper'><div id='news_header'><h3>". $title ."</h3></div><div id='news_content'>". $text ."</div><div id='news_footer'>". $info ."</div></div>". $currententries); 
    	        fclose($fp);        
    	        
    	        $image_name = "test";
    			$myPhoto = new Uploaded('image'); 
    			$photoFileName = $myPhoto -> getFileName('images/', $image_name);
    	        
    	        header('Location: ?page=home');
            }
            else{
            	header('Location: ?page=admin&id=news&title='. $title .'&text='. $text .'#anc');
            }     	
     	}
     	else{
     		header('Location: ?page=home');
    	}
    ?>
    Code (markup):
    EDIT: I just received a mail from my host, it's a problem at their end.. code works.. sry for post :$
     
    fex, Mar 4, 2009 IP