Jump to content

Image file is not uploading


Chrisj

Recommended Posts

I don't know why this image upload code isn't uploading the image file to the uploads/ folder.

Any help will be appreciated.

	$allowedExts = array("gif", "jpeg", "jpg", "png");
	$temp = explode(".", $_FILES["file"]["name"]);
	$extension = strtolower( end($temp) );
	if (  $_FILES["file"]["size"] < 2000000
	&& in_array($extension, $allowedExts) )
	{
	if ($_FILES["file"]["error"]!= 0)
	{
	echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
	} else {
	$length = 20;
	$randomString = substr(str_shuffle(md5(time())),0,$length);
	$newfilename = $randomString . "." . $extension;
	move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newfilename );
	$file_location = '<a href="http://www.--.com/upload/' . $newfilename . '">http://www.--.com/upload/' . $newfilename . '</a>';
	}
	} else {
	echo "Invalid upload file";
	}
<form enctype="multipart/form-data" action="uploader.php">
<input type="file" name="file" id="file">
<input type="submit" name="submitted" value="Submit">
</form>
Link to comment
https://forums.phpfreaks.com/topic/291476-image-file-is-not-uploading/
Share on other sites

This change now gets the image uploaded, however this code takes me to the next page.

I'd like to figure out how to stay on the same page after upload. Any help will be appreciated.

<form enctype="multipart/form-data" action="" method="POST">
<input type="file" name="file" id="file">
<input class="upload-input" type="hidden" name="form_submitted" value="yes" />
<input type="submit" value="submit" />
</form>

Without reloading the page, you mean? That's AJAX, which is simple to implement... except for doing file uploads. Which isn't actually AJAX but whatever.

 

Find yourself a Javascript library that can do file uploads and implement that in your site. Your PHP doesn't have to change much except it shouldn't output HTML (not when it's handling the file upload).

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.