How do i accept picture uploads unto a php webpage from a user?

Recommended Answers

All 2 Replies

Its the same way as any file upload with just restrictions about what type are accepted.

google w3school and look in the php section. That will probably help you a lot.

Member Avatar for P0lT10n

This is an example of an image upload

Validation code

// Recibe from post
$file_name = $_FILES["image"]["name"];
$file_type = $_FILES["image"]["type"];
$file_size = $_FILES["image"]["size"];
$file_error = $_FILES["image"]["error"];
$file_tmp = $_FILES["image"]["tmp_name"];

// Validate Image
if(!empty($file_name)){
	if($file_name > 120000){
		$msg .= "El peso supera los 120 kb !<br>";
	}
	if(!ereg(".gif",$file_name) && !ereg(".jpg",$file_name) && !ereg(".jpeg",$file_name)){
		$msg .= "The extension is invalid.<br>";
	}
	if($archivo_error > 0){
		$msg .= "Error when uploading.<br>";
	}
	if(file_exists("images/".$file_name)){
		$msg .= "The photo already exsists with that name.<br>";
	}
}

Only accepts .gif or .jpg or .jpeg...

And this is the upload code when is photo is valid

if(!empty($file_name)){
    echo "Uploading: ".$file_name."<br>";
    move_uploaded_file($file_tmp,"images/".$file_name);
}

And don't forget to put enctype="multipart/form-data" to upload a file

<form name="f1" action="" method="post" enctype="multipart/form-data">
    <input type="file" name="image" size="75" />
    <input type="submit" value="Send Photo"/>
</form>
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.