Jump to content

Simple Image Uploader


LemonInflux

Recommended Posts

If u want just some directions then:

 

- Make a form with a: enctype="multipart/form-data" and a file field

- Use $_FILES for file names, size etc

- Use move_uploaded_file() to upload the file in your directory

- Return the file name u set it.

 

If u want a script that works then i can write a simple one for u.

Link to comment
Share on other sites

jesirose and matthewhaworth have it right, as this isnt a request forum, instead a learning one. Anyway as this was quick and easy, here u go:

 

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

<?php
if($_FILES['file']['name'] != ""){
$filename = $_FILES['file']['name'];
$ext = strtolower(substr(strrchr($filename, '.'), 1));
$imagesExt = array('jpg', 'jpeg', 'png', 'gif', 'bmp');
$newPath = "mynewdir/{$filename}";
if(move_uploaded_file($_FILES['file']['tmp_name'], $newPath)){
	echo "File was uploaded succesfully<br />";
	if(in_array($ext, $imagesExt)){ 
		echo "Here is the image u uploaded";
		echo "<img src=\"{$newPath}\" />";
	}
}else{
	echo "There was an error uploading the file";
}
}
?>

 

Not tested though. If it has any syntax errors (missing " or ; or } or whatever) try debugging it yourself.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.