Jump to content

Recommended Posts

Basically, I need a code that will upload images to a directory on my server, and when a user submits a picture, a link to the picture is returned to them. Any ideas?

 

Good some tutorials.  We are not here to write code.

Link to comment
https://forums.phpfreaks.com/topic/67150-simple-image-uploader/#findComment-336779
Share on other sites

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
https://forums.phpfreaks.com/topic/67150-simple-image-uploader/#findComment-336782
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
https://forums.phpfreaks.com/topic/67150-simple-image-uploader/#findComment-336790
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.