How might I upload a image file?

General discussions related to php

Moderators: macek, egami, gesf

Post Reply
IChaps
New php-forum User
New php-forum User
Posts: 86
Joined: Tue Mar 01, 2016 3:07 pm

How might I upload a image file?

Post by IChaps » Fri Jan 06, 2017 3:53 pm

Hello.

Can I please briefly ask? I've tried to upload a picture image (gif and jpg). The two image files are less than 2mb.
However for some reason, I managed to upload 1 image, and when I tried again an hour later the script has stopped working.
I'm not sure why this is.

I've got a folder located at /images and a second at /Core/user/images . I've tried changing the move_uploaded_file($file_tmp,"images/".$file_name); to point to both locatations (one at a time), and no file is uploaded.

Have I gone wrong, or changed something I should have left alone?

Thanks.


Code: Select all

<?php
/*
* 	   Simple file Upload system with PHP.
* 	   Created By Tech Stream
* 	   Original Source at http://techstream.org/Web-Development/PHP/Single-File-Upload-With-PHP
*      This program is free software; you can redistribute it and/or modify
*      it under the terms of the GNU General Public License as published by
*      the Free Software Foundation; either version 2 of the License, or
*      (at your option) any later version.
*
*      This program is distributed in the hope that it will be useful,
*      but WITHOUT ANY WARRANTY; without even the implied warranty of
*      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*      GNU General Public License for more details.
*
*/
	if(isset($_FILES['image'])){
		$errors= array();
		$file_name = $_FILES['image']['name'];
		$file_size =$_FILES['image']['size'];
		$file_tmp =$_FILES['image']['tmp_name'];
		$file_type=$_FILES['image']['type'];
		$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));

		$expensions= array("jpeg","jpg","png");
		if(in_array($file_ext,$expensions)=== false){
			$errors[]="extension not allowed, please choose a JPEG or PNG file.";
		}
		if($file_size > 2097152){
		$errors[]='File size must be excately 2 MB';
		}				
		if(empty($errors)==true){
			move_uploaded_file($file_tmp,"images/".$file_name);
			echo "Success";
		}else{
			print_r($errors);
		}
	}
?>

<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>

AdoptiveSolution
New php-forum User
New php-forum User
Posts: 122
Joined: Wed Jun 15, 2016 8:35 am

Re: How might I upload a image file?

Post by AdoptiveSolution » Sun Jan 08, 2017 5:57 am

Here you can get the fully functional code to upload an image :

http://www.w3schools.com/php/php_file_upload.asp

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest