# Image upload not working.

**URL:** https://forum.infinityfree.com/t/image-upload-not-working/7106
**Category:** Hosting Support
**Created:** [July 28, 2017, 11:25am UTC](https://forum.infinityfree.com/t/image-upload-not-working/7106 "2017-07-28T11:25:01Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ckhawand](https://forum.infinityfree.com/letter_avatar/ckhawand/32/5_5575768a8748004e209b776fc1b2916d.png) [@ckhawand](https://forum.infinityfree.com/u/ckhawand)
#### Post date: [July 28, 2017, 11:25am UTC](https://forum.infinityfree.com/t/image-upload-not-working/7106/1 "2017-07-28T11:25:01Z")

</div>

So I grabbed an image upload code from w3schools.  
Everything worked fine on other hosts, but here, I am unable to upload the image through , I have set the form to multiform.  
Here’s the php code

```auto
 if(isset($_POST['new'])){
 $target_dir = "/image/";
 $uploadOk = 1;
 $info = getimagesize($_FILES['fileToUpload']['tmp_name']);
 $imageFileType = $info['mime'];
 $target_file = $target_dir . rand(1000000,9999999) . $imageFileType;
	 // Check if image file is a actual image or fake image
		 $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
		 if($check !== false) {
			 $uploadOk = 1;
		 } else {
			 $errMSG .= "File is not an image.";
			 $uploadOk = 0;
		 }
	 // Check if file already exists
	 if (file_exists($target_file)) {
		 echo "Sorry, file already exists.";
		 $uploadOk = 0;
	 }
	 // Check file size
	 if ($_FILES["fileToUpload"]["size"] > 2000000) {
		 $errMSG .= "Sorry, your file is too large.";
		 $uploadOk = 0;
	 }
	 // Allow certain file formats
	 if($imageFileType != "image/jpg" && $imageFileType != "image/png" && $imageFileType != "image/jpeg"
	 && $imageFileType != "image/gif" ) {
		 $errMSG .= "Sorry, only JPG, JPEG, PNG & GIF files are allowed.".$imageFileType;
		 $uploadOk = 0;
	 }
	 // Check if $uploadOk is set to 0 by an error
	 if ($uploadOk == 0) {
		 $errMSG .= "Sorry, your file was not uploaded.";
	 // if everything is ok, try to upload file
	 } else {
		 if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
			  if(isset($_POST['status'])){
				$status = '1';
			  }else{
				$status = '0';
			  }
			  $image = $target_file;
			  $ename = $_POST['ename'];
			  $location = $_POST['loc'];
			  $query = mysqli_query($conn,"INSERT INTO `events` (name,user,views,checkins,status,location) values($ename,$id,'0','0','$status','$loc')");
			  header('Location: status.php?ref=new&success=1');
			  exit;
		 }else{
			 $errMSG = "Sorry, there was an error uploading your file.";
		 }
	 }
 }

```

---

<div class="post-metadata">

### Author: ![Admin](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/admin/32/36437_2.png) [@Admin](https://forum.infinityfree.com/u/Admin)
#### Post date: [July 28, 2017, 3:30pm UTC](https://forum.infinityfree.com/t/image-upload-not-working/7106/2 "2017-07-28T15:30:39Z")

</div>

You’re “unable to upload the image through”? I see lots of error checking in the script, do you see any of those errors when trying to upload the image? Or do you see something else?

---

<div class="post-metadata">

### Author: ![ckhawand](https://forum.infinityfree.com/letter_avatar/ckhawand/32/5_5575768a8748004e209b776fc1b2916d.png) [@ckhawand](https://forum.infinityfree.com/u/ckhawand)
#### Post date: [July 28, 2017, 5:29pm UTC](https://forum.infinityfree.com/t/image-upload-not-working/7106/3 "2017-07-28T17:29:53Z")

</div>

Nope, I directly see “Sorry, there was an error uploading your file.”  
So there is something wrong on the server side…

---

<div class="post-metadata">

### Author: ![Admin](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/admin/32/36437_2.png) [@Admin](https://forum.infinityfree.com/u/Admin)
#### Post date: [July 28, 2017, 5:56pm UTC](https://forum.infinityfree.com/t/image-upload-not-working/7106/4 "2017-07-28T17:56:03Z")

</div>

> @ckhawand said:  
> Nope, I directly see “Sorry, there was an error uploading your file.”  
> So there is something wrong on the server side…

It’s a server side script, so yes, it’s probably something server side.

Judging by the stage where that error could occur, I’d say the issue is that your script is unable to move the image to the target directory.

Your target directory is set to `/images/`. However, note that any file directory is relative to the root of your account, not of your website. And PHP can’t access files outside the website directory so it cannot upload the image to the target directory.

You could change the image path to be an absolute path within your website (using `/htdocs/images/` would probably work if the domain is your primary account domain) or you could use a relative path (like `images/`, so without the first slash).

---

<div class="post-metadata">

### Author: ![ckhawand](https://forum.infinityfree.com/letter_avatar/ckhawand/32/5_5575768a8748004e209b776fc1b2916d.png) [@ckhawand](https://forum.infinityfree.com/u/ckhawand)
#### Post date: [July 29, 2017, 6:23am UTC](https://forum.infinityfree.com/t/image-upload-not-working/7106/5 "2017-07-29T06:23:57Z")

</div>

Okay, I’m going to try…

---

<div class="post-metadata">

### Author: ![system](https://infinityfree-forum-uploads.s3.dualstack.eu-central-1.amazonaws.com/original/3X/b/0/b024fd3fa88bae093c9576e55c2c94e33f890206.svg) [@system](https://forum.infinityfree.com/u/system)
#### Post date: [January 17, 2019, 7:28pm UTC](https://forum.infinityfree.com/t/image-upload-not-working/7106/6 "2019-01-17T19:28:24Z")

</div>


