Jump to content

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).

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.