Jump to content

Cannot upload files


Fabis94

Recommended Posts

Here's the script:

 

   if (!isset($_POST['name_added_get']) && !isset($_POST['write_cont']) && !isset($_POST['second_method'])) {
   echo '<br><br><hr>You can also upload a page instead of making a new one. Select the file below, select its destination ';
   echo 'and press "Upload File"<br>';
echo "<form action='upload_file.php' method='post'";
echo "enctype='multipart/form-data'>";
echo "<label for='file'>Filename:</label>";
echo "<input type='file' name='file' id='file' />"; 
echo "<br /><input type='hidden' name='MAX_FILE_SIZE' value='30000' /><br><input name='upload_dir' type='text' value='/' />";
echo "<input type='submit' name='upload' value='Submit' />";
echo "</form>";
   if (isset($_POST['upload'])) {//If Upload Button is Pressed
   		$upload_dir = $_POST['upload_dir'];
	   if ($_POST['upload_dir'] != '/') {
			if (substr($_POST['upload_dir'], -1) != '/' || substr($_POST['upload_dir'], 0, 1) != '/') {
				echo '<script language"text/javascript">alert("Folder Location must start and end with /");</script>';
				echo '<script type="text/javascript">window.location="newpage.php";</script>';
				exit;
			}
	   }//Correct Folder IF Ends
		if (file_exists($def_path . $_POST['upload_dir'])) {//ABC:  /home/content/f/a/b/fabis94/html
				$filepath = $def_path . $_POST['upload_dir'] . basename($_FILES["file"]["name"]);
				$filename = basename($_FILES["file"]["name"]);
				  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
				  echo "Type: " . $_FILES["file"]["type"] . "<br />";
				  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
				  echo "Stored in: " . $_FILES["file"]["tmp_name"];

				echo 'Up dir: ' . $_POST['upload_dir'] . ' Original Basename: ' . $_FILES["file"]["name"] . 'Filepath: ' . $filepath . ' <br>';
				if (!file_exists($filepath)) {//If PHPNAME doesn't and FOLDERLOCATION exists
					if (substr($filename, -4) == '.php') {//If it is a PHP file
						if (move_uploaded_file($_FILES["file"]["tmp_name"], $filepath)) {
							echo "<br><font color='green'>File is valid, and was successfully uploaded.</font>\n";
						} else {
							echo "<br><font color='red'>Couldn't upload and move.</font>\n";
						}
					} else {
						echo '<script language"text/javascript">alert("File must be .PHP");</script>';
						echo '<script type="text/javascript">window.location="newpage.php";</script>';	
					}
				} else {
					echo '<script language"text/javascript">alert("Page already exists in the specified directory");</script>';
					/*echo '<script type="text/javascript">window.location="newpage.php";</script>';*/
					exit;
				}//PHPNAME Exists IF ends here
		} else {
			echo '<script language"text/javascript">alert("The directory you specified doesnt exist");</script>';
			echo '<script type="text/javascript">window.location="newpage.php";</script>';
			exit;
		}//FOLDERLOCATION Exists IF ends here
   }//Upload Button IF ends
   }//Upload method ends

 

It just simpy says "Folder Location must start and end with /" in the pop-up window and also in this part:

 

					  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
				  echo "Type: " . $_FILES["file"]["type"] . "<br />";
				  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
				  echo "Stored in: " . $_FILES["file"]["tmp_name"];

 

It shows nothing. That means it didn't upload it and something is maybe wrong with my temp upload dir, but i use GoDaddy Shared Hosting and i don't have a php.ini file on the root (so it uses the default configs) and i cannot change the directory, but i used ini_get() and found out it's /tmp

and the path to my actual site is: /home/content/f/a/b/XXXX/html/

So it is even before the 'home' directory and i cannot access that. Anyways:

 

1. There is something wrong with my script and please help me fix it :P

2. Can anyone give a php.ini file with all the default configs set so i can change them after.

Link to comment
https://forums.phpfreaks.com/topic/152636-cannot-upload-files/
Share on other sites

I don't understand. At the beginning of my PHP script i already have:

 

$def_path = '/home/content/f/a/b/fabis94/html';

 

Also if you're talking about the location that is $upload_dir then it's the directory where the file should be uploaded (you select your file, write in the directory where it should be uploaded and press UPLOAD). And the error i mentioned before is because of this thing. Also it says "Page already exists in this directory." not the error i mentioned before (sorry). And the error happens if the directory chosen doesn't end OR/AND start with '/' or if the directory is '/' (root) it doesn't give any errors. But it does!

 

Also here is the WHOLE script (i gave u the uploading part only, but this part has also MAKING a new php file not uploading it) maybe something is interfering with something.

 

<?php
  $htmlstart = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>

</body>
</html>';
  $def_path = "/home/content/f/a/b/fabis94/html";
  if (isset($_POST['name_added_get'])) { //Checks if the name is already set from the INDEX page
	if ($_POST['folder_location'] != '/') {
		if (substr($_POST['folder_location'], -1) != '/' || substr($_POST['folder_location'], 0, 1) != '/') {
			echo '<script language"text/javascript">alert("Folder Location must start and end with /");</script>';
			echo '<script type="text/javascript">window.location="newpage.php";</script>';
			exit;
		}
	}
	if (substr($_POST['phpname'], -1) == '/' || substr($_POST['phpname'], 0, 1) == '/') {
		echo '<script language"text/javascript">alert("Page name must not start or end with /");</script>';
		echo '<script type="text/javascript">window.location="newpage.php";</script>';
		exit;
	}
	$dom_path = 'http://www.mechtopia.net' . substr($_POST['folder_location'], -$a) . $_POST['phpname'] . '.php';
  		if (file_exists($def_path . $_POST['folder_location'])) {//ABC:  /home/content/f/a/b/fabis94/html
			$filepath = $def_path . $_POST['folder_location'] . $_POST['phpname'] . '.php';
			if (!file_exists($filepath)) {//If PHPNAME doesn't and FOLDERLOCATION exists
					$fopen = fopen($filepath, "w");
					fwrite($fopen, $htmlstart);
					fclose($fopen);
					echo '<form action="newpage.php" method="post">';
					echo 'Write the contents of the page: <br>';
	echo '<center><textarea name="page_contents" cols="60" rows="10">' . file_get_contents($filepath) . '</textarea>';
					echo '<input name="h_filepath" type="hidden" value="' . $filepath . '"/>';
					$dom_path = 'http://www.mechtopia.net' . substr($_POST['folder_location'], -$a) . $_POST['phpname'] . '.php';
					echo '<input name="dom_path" type="hidden" value="' . $dom_path . '"/>';
					echo '<br><input value="Write contents to the page!" name="write_cont" type="submit" />';
					echo '</form>';
			} else {
				echo '<font color="red">Page already exists in the specified directory.</font>';
			}//PHPNAME Exists IF ends here
	} else {
		echo '<font color="red">The directory you specified doesnt exist.</font>';
	}//FOLDERLOCATION Exists IF ends here
  }
  
  if (isset($_POST['write_cont'])) { //If contents are ready being added - INDEX Method
  		$opened_page = fopen($_POST['h_filepath'], "w") or die('Could not open the page that was made before.');
	fwrite($opened_page, $_POST['page_contents']) or die('Could not write into the file.');
	fclose($opened_page);
	echo '<script type="text/javascript">window.location="' . $_POST['dom_path'] . '";</script>';
  }
  /*---------------------------INDEX Method Ends Here---------------------------------*/
  if (!isset($_POST['name_added_get']) && !isset($_POST['write_cont']) && !isset($_POST['second_method'])) {
  		echo '<form action="newpage.php" method="post"><b>Create a new page - Enter its name (without .php):</b> ';
  		echo '<input name="phpname2" type="text" size="15" maxlength="10" />';
	echo '<br /><b>Write in the folder where it should be saved*:</b> <input name="folder_location2" type="text" value="/" size="15">';
	echo '<br>*Root of the server is "/" so if you want to save it on a folder called PHP, write "/PHP/".';
	echo ' Always add the end-slash.<br><br><br><br>';
	echo '<center><b>Write in the contents of the file:</b> ';
	echo '<textarea name="page_contents2" cols="60" rows="10">' . $htmlstart . '</textarea>';
	echo '<br><input name="second_method" type="submit" /></center>';
  }
  
  if (isset($_POST['second_method'])) {//Second Methods last part
  		if ($_POST['folder_location2'] != '/') {
		if (substr($_POST['folder_location2'], -1) != '/' || substr($_POST['folder_location2'], 0, 1) != '/') {
			echo '<script language"text/javascript">alert("Folder Location must start and end with /");</script>';
			echo '<script type="text/javascript">window.location="newpage.php";</script>';
			exit;
		}
	}
	if (substr($_POST['phpname2'], -1) == '/' || substr($_POST['phpname2'], 0, 1) == '/') {
		echo '<script language"text/javascript">alert("Page name must not start or end with /");</script>';
		echo '<script type="text/javascript">window.location="newpage.php";</script>';
		exit;
	}
	$dom_path2 = 'http://www.mechtopia.net' . substr($_POST['folder_location2'], -$a) . $_POST['phpname2'] . '.php';
	if (file_exists($def_path . $_POST['folder_location2'])) {//ABC:  /home/content/f/a/b/fabis94/html
			$filepath = $def_path . $_POST['folder_location2'] . $_POST['phpname2'] . '.php';
			if (!file_exists($filepath)) {//If PHPNAME doesn't and FOLDERLOCATION exists
				$dom_path2 = 'http://www.mechtopia.net' . substr($_POST['folder_location2'], -$a) . $_POST['phpname2'] . '.php';
					$fopen2 = fopen($filepath, "w");
					fwrite($fopen2, $_POST['page_contents2']);
					fclose($fopen2);
					echo '<script type="text/javascript">window.location="' . $dom_path2 . '";</script>';
			} else {
				echo '<script language"text/javascript">alert("Page already exists in the specified directory");</script>';
				echo '<script type="text/javascript">window.location="newpage.php";</script>';
				exit;
			}//PHPNAME Exists IF ends here
	} else {
		echo '<script language"text/javascript">alert("The directory you specified doesnt exist");</script>';
		echo '<script type="text/javascript">window.location="newpage.php";</script>';
		exit;
	}//FOLDERLOCATION Exists IF ends here
  }/*----------------------------------2nd Method Ends here---------------------------------*/
   if (!isset($_POST['name_added_get']) && !isset($_POST['write_cont']) && !isset($_POST['second_method'])) {
   echo '<br><br><hr>You can also upload a page instead of making a new one. Select the file below, select its destination ';
   echo 'and press "Upload File"<br>';
echo "<form action='upload_file.php' method='post'";
echo "enctype='multipart/form-data'>";
echo "<label for='file'>Filename:</label>";
echo "<input type='file' name='file' id='file' />"; 
echo "<br /><input type='hidden' name='MAX_FILE_SIZE' value='30000' /><br><input name='upload_dir' type='text' value='/' />";
echo "<input type='submit' name='upload' value='Submit' />";
echo "</form>";
   if (isset($_POST['upload'])) {//If Upload Button is Pressed
   		$upload_dir = $_POST['upload_dir'];
	   if ($_POST['upload_dir'] != '/') {
			if (substr($_POST['upload_dir'], -1) != '/' || substr($_POST['upload_dir'], 0, 1) != '/') {
				echo '<script language"text/javascript">alert("Folder Location must start and end with /");</script>';
				echo '<script type="text/javascript">window.location="newpage.php";</script>';
				exit;
			}
	   }//Correct Folder IF Ends
		if (file_exists($def_path . $_POST['upload_dir'])) {//ABC:  /home/content/f/a/b/fabis94/html
				$filepath = $def_path . $_POST['upload_dir'] . basename($_FILES["file"]["name"]);
				$filename = basename($_FILES["file"]["name"]);
				  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
				  echo "Type: " . $_FILES["file"]["type"] . "<br />";
				  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
				  echo "Stored in: " . $_FILES["file"]["tmp_name"];
				  echo "Errors(?): " . $_FILES["file"]["error"];

				echo 'Up dir: ' . $_POST['upload_dir'] . ' Original Basename: ' . $_FILES["file"]["name"] . 'Filepath: ' . $filepath . ' <br>';
				if (!file_exists($filepath)) {//If PHPNAME doesn't and FOLDERLOCATION exists
					if (substr($filename, -4) == '.php') {//If it is a PHP file
						if (move_uploaded_file($_FILES["file"]["tmp_name"], $filepath)) {
							echo "<br><font color='green'>File is valid, and was successfully uploaded.</font>\n";
						} else {
							echo "<br><font color='red'>Couldn't upload and move.</font>\n";
						}
					} else {
						echo '<script language"text/javascript">alert("File must be .PHP");</script>';
						echo '<script type="text/javascript">window.location="newpage.php";</script>';	
					}
				} else {
					echo '<script language"text/javascript">alert("Page already exists in the specified directory");</script>';
					/*echo '<script type="text/javascript">window.location="newpage.php";</script>';*/
					exit;
				}//PHPNAME Exists IF ends here
		} else {
			echo '<script language"text/javascript">alert("The directory you specified doesnt exist");</script>';
			echo '<script type="text/javascript">window.location="newpage.php";</script>';
			exit;
		}//FOLDERLOCATION Exists IF ends here
   }//Upload Button IF ends
   }//Upload method ends
  
  ?>

Link to comment
https://forums.phpfreaks.com/topic/152636-cannot-upload-files/#findComment-801745
Share on other sites

I don't understand. At the beginning of my PHP script i already have:

 

$def_path = '/home/content/f/a/b/fabis94/html';

 

Also if you're talking about the location that is $upload_dir then it's the directory where the file should be uploaded (you select your file, write in the directory where it should be uploaded and press UPLOAD). And the error i mentioned before is because of this thing. Also it says "Page already exists in this directory." not the error i mentioned before (sorry). And the error happens if the directory chosen doesn't end OR/AND start with '/' or if the directory is '/' (root) it doesn't give any errors. But it does!

 

Also here is the WHOLE script (i gave u the uploading part only, but this part has also MAKING a new php file not uploading it) maybe something is interfering with something.

 

<?php
  $htmlstart = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>

</body>
</html>';
  $def_path = "/home/content/f/a/b/fabis94/html";
  if (isset($_POST['name_added_get'])) { //Checks if the name is already set from the INDEX page
	if ($_POST['folder_location'] != '/') {
		if (substr($_POST['folder_location'], -1) != '/' || substr($_POST['folder_location'], 0, 1) != '/') {
			echo '<script language"text/javascript">alert("Folder Location must start and end with /");</script>';
			echo '<script type="text/javascript">window.location="newpage.php";</script>';
			exit;
		}
	}
	if (substr($_POST['phpname'], -1) == '/' || substr($_POST['phpname'], 0, 1) == '/') {
		echo '<script language"text/javascript">alert("Page name must not start or end with /");</script>';
		echo '<script type="text/javascript">window.location="newpage.php";</script>';
		exit;
	}
	$dom_path = 'http://XXX' . substr($_POST['folder_location'], -$a) . $_POST['phpname'] . '.php';
  		if (file_exists($def_path . $_POST['folder_location'])) {//ABC:  /home/content/f/a/b/fabis94/html
			$filepath = $def_path . $_POST['folder_location'] . $_POST['phpname'] . '.php';
			if (!file_exists($filepath)) {//If PHPNAME doesn't and FOLDERLOCATION exists
					$fopen = fopen($filepath, "w");
					fwrite($fopen, $htmlstart);
					fclose($fopen);
					echo '<form action="newpage.php" method="post">';
					echo 'Write the contents of the page: <br>';
	echo '<center><textarea name="page_contents" cols="60" rows="10">' . file_get_contents($filepath) . '</textarea>';
					echo '<input name="h_filepath" type="hidden" value="' . $filepath . '"/>';
					$dom_path = 'http://www.yus.net' . substr($_POST['folder_location'], -$a) . $_POST['phpname'] . '.php';
					echo '<input name="dom_path" type="hidden" value="' . $dom_path . '"/>';
					echo '<br><input value="Write contents to the page!" name="write_cont" type="submit" />';
					echo '</form>';
			} else {
				echo '<font color="red">Page already exists in the specified directory.</font>';
			}//PHPNAME Exists IF ends here
	} else {
		echo '<font color="red">The directory you specified doesnt exist.</font>';
	}//FOLDERLOCATION Exists IF ends here
  }
  
  if (isset($_POST['write_cont'])) { //If contents are ready being added - INDEX Method
  		$opened_page = fopen($_POST['h_filepath'], "w") or die('Could not open the page that was made before.');
	fwrite($opened_page, $_POST['page_contents']) or die('Could not write into the file.');
	fclose($opened_page);
	echo '<script type="text/javascript">window.location="' . $_POST['dom_path'] . '";</script>';
  }
  /*---------------------------INDEX Method Ends Here---------------------------------*/
  if (!isset($_POST['name_added_get']) && !isset($_POST['write_cont']) && !isset($_POST['second_method'])) {
  		echo '<form action="newpage.php" method="post"><b>Create a new page - Enter its name (without .php):</b> ';
  		echo '<input name="phpname2" type="text" size="15" maxlength="10" />';
	echo '<br /><b>Write in the folder where it should be saved*:</b> <input name="folder_location2" type="text" value="/" size="15">';
	echo '<br>*Root of the server is "/" so if you want to save it on a folder called PHP, write "/PHP/".';
	echo ' Always add the end-slash.<br><br><br><br>';
	echo '<center><b>Write in the contents of the file:</b> ';
	echo '<textarea name="page_contents2" cols="60" rows="10">' . $htmlstart . '</textarea>';
	echo '<br><input name="second_method" type="submit" /></center>';
  }
  
  if (isset($_POST['second_method'])) {//Second Methods last part
  		if ($_POST['folder_location2'] != '/') {
		if (substr($_POST['folder_location2'], -1) != '/' || substr($_POST['folder_location2'], 0, 1) != '/') {
			echo '<script language"text/javascript">alert("Folder Location must start and end with /");</script>';
			echo '<script type="text/javascript">window.location="newpage.php";</script>';
			exit;
		}
	}
	if (substr($_POST['phpname2'], -1) == '/' || substr($_POST['phpname2'], 0, 1) == '/') {
		echo '<script language"text/javascript">alert("Page name must not start or end with /");</script>';
		echo '<script type="text/javascript">window.location="newpage.php";</script>';
		exit;
	}
	$dom_path2 = 'http://www.yus.net' . substr($_POST['folder_location2'], -$a) . $_POST['phpname2'] . '.php';
	if (file_exists($def_path . $_POST['folder_location2'])) {//ABC:  /home/content/f/a/b/fabis94/html
			$filepath = $def_path . $_POST['folder_location2'] . $_POST['phpname2'] . '.php';
			if (!file_exists($filepath)) {//If PHPNAME doesn't and FOLDERLOCATION exists
				$dom_path2 = 'http://www.yus.net' . substr($_POST['folder_location2'], -$a) . $_POST['phpname2'] . '.php';
					$fopen2 = fopen($filepath, "w");
					fwrite($fopen2, $_POST['page_contents2']);
					fclose($fopen2);
					echo '<script type="text/javascript">window.location="' . $dom_path2 . '";</script>';
			} else {
				echo '<script language"text/javascript">alert("Page already exists in the specified directory");</script>';
				echo '<script type="text/javascript">window.location="newpage.php";</script>';
				exit;
			}//PHPNAME Exists IF ends here
	} else {
		echo '<script language"text/javascript">alert("The directory you specified doesnt exist");</script>';
		echo '<script type="text/javascript">window.location="newpage.php";</script>';
		exit;
	}//FOLDERLOCATION Exists IF ends here
  }/*----------------------------------2nd Method Ends here---------------------------------*/
   if (!isset($_POST['name_added_get']) && !isset($_POST['write_cont']) && !isset($_POST['second_method'])) {
   echo '<br><br><hr>You can also upload a page instead of making a new one. Select the file below, select its destination ';
   echo 'and press "Upload File"<br>';
echo "<form action='upload_file.php' method='post'";
echo "enctype='multipart/form-data'>";
echo "<label for='file'>Filename:</label>";
echo "<input type='file' name='file' id='file' />"; 
echo "<br /><input type='hidden' name='MAX_FILE_SIZE' value='30000' /><br><input name='upload_dir' type='text' value='/' />";
echo "<input type='submit' name='upload' value='Submit' />";
echo "</form>";
   if (isset($_POST['upload'])) {//If Upload Button is Pressed
   		$upload_dir = $_POST['upload_dir'];
	   if ($_POST['upload_dir'] != '/') {
			if (substr($_POST['upload_dir'], -1) != '/' || substr($_POST['upload_dir'], 0, 1) != '/') {
				echo '<script language"text/javascript">alert("Folder Location must start and end with /");</script>';
				echo '<script type="text/javascript">window.location="newpage.php";</script>';
				exit;
			}
	   }//Correct Folder IF Ends
		if (file_exists($def_path . $_POST['upload_dir'])) {//ABC:  /home/content/f/a/b/fabis94/html
				$filepath = $def_path . $_POST['upload_dir'] . basename($_FILES["file"]["name"]);
				$filename = basename($_FILES["file"]["name"]);
				  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
				  echo "Type: " . $_FILES["file"]["type"] . "<br />";
				  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
				  echo "Stored in: " . $_FILES["file"]["tmp_name"];
				  echo "Errors(?): " . $_FILES["file"]["error"];

				echo 'Up dir: ' . $_POST['upload_dir'] . ' Original Basename: ' . $_FILES["file"]["name"] . 'Filepath: ' . $filepath . ' <br>';
				if (!file_exists($filepath)) {//If PHPNAME doesn't and FOLDERLOCATION exists
					if (substr($filename, -4) == '.php') {//If it is a PHP file
						if (move_uploaded_file($_FILES["file"]["tmp_name"], $filepath)) {
							echo "<br><font color='green'>File is valid, and was successfully uploaded.</font>\n";
						} else {
							echo "<br><font color='red'>Couldn't upload and move.</font>\n";
						}
					} else {
						echo '<script language"text/javascript">alert("File must be .PHP");</script>';
						echo '<script type="text/javascript">window.location="newpage.php";</script>';	
					}
				} else {
					echo '<script language"text/javascript">alert("Page already exists in the specified directory");</script>';
					/*echo '<script type="text/javascript">window.location="newpage.php";</script>';*/
					exit;
				}//PHPNAME Exists IF ends here
		} else {
			echo '<script language"text/javascript">alert("The directory you specified doesnt exist");</script>';
			echo '<script type="text/javascript">window.location="newpage.php";</script>';
			exit;
		}//FOLDERLOCATION Exists IF ends here
   }//Upload Button IF ends
   }//Upload method ends
  
  ?>

 

Don't mind the different domain names they are all the same in the real script.

 

*Ooops sorry for double post. Mistook modify with Quote.

 

Also the error happens if (!file_exists($filepath)) and $filepath is $filepath = $def_path . $_POST['upload_dir'] . basename($_FILES["file"]["name"]);

 

But as it later says at "Upload: " . $_FILES["file"]["name"] Upload: (nothing)

That means it didn't even get the file. Also i added echo "Errors(?): " . $_FILES["file"]["error"]; and it returned nothing too.

Link to comment
https://forums.phpfreaks.com/topic/152636-cannot-upload-files/#findComment-801748
Share on other sites

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.