Jump to content

Recommended Posts

Hi!

 

I'm very, very new to PHP scripting so please bear with me if my questions seems stupid.

 

I am using htaccess 5rto password-protect a directory on my site, and I would like to have the ability to upload files to that directory without using ftp.

 

I found a upload script and modified it to my liking but theres one thing that keeps bugging me. Everytime I upload something, it outputs some text to tell me wether it has succeded or failed - but the output shows on an "empty" page and not on the page where the upload-form is. In the address bar, I noticed it changes name to "domain.dk/sec/upload_script.php" when it shows the the output.

 

If I just include the whole script in my index.php (where the html upload form also is) it actually works - but then I'm getting an 'please choose a file to upload'-error message, ofcourse because it 'runs' the script as it reads index.php.

 

Now, I do understand the reason why this is happening, but I don't know how to solve it. I tried finding help on google, but I don't know what to search for.

 

What I would like is the output to be shown just under the upload form (when there is one).

 

What do I do here? Any help would be greatly appreciated!

Link to comment
https://forums.phpfreaks.com/topic/265401-show-output-of-script-in-indexphp/
Share on other sites

Sorry, here's the code:

 

 

Index.php:

<!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-Language" content="da" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
.style1 {
text-align: left;
}
</style>
</head>

<body>
<p>UPLOAD FILE</p>
<form enctype="multipart/form-data" method="post" action="file_upload_script.php">
<div class="style1">
Choose file:<br />
<input name="file1" type="file" /><br />
<input type="submit" value="Go" />
<input type="reset" value="Reset" />
</div>
</form>
</body>
<?php include("file_upload_script.php"); ?>

 

 

file_upload_script.php:

<?php
// Set local PHP vars from the POST vars sent from our form using the array
// of data that the $_FILES global variable contains for this uploaded file
$fileName = $_FILES["file1"]["name"]; // The file name
$fileTmpLoc = $_FILES["file1"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["file1"]["type"]; // The type of file it is
$fileSize = $_FILES["file1"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["file1"]["error"]; // 0 for false... and 1 for true
// Specific Error Handling if you need to run error checking
if (!$fileTmpLoc) { // if file not chosen
    echo "ERROR: Please browse for a file before clicking the upload button.";
    exit();
} else if($fileSize > 524288000) { // if file is larger than we want to allow
    echo "ERROR: File size restriction. File larger than 500mb.";
    unlink($fileTmpLoc);
    exit();
}
// Place it into your "uploads" folder mow using the move_uploaded_file() function 
move_uploaded_file($fileTmpLoc, "test/$fileName");
// Check to make sure the uploaded file is in place where you want it
if (!file_exists("test/$fileName")) {
    echo "ERROR: File not uploaded. $filename alreadt exists.<br /><br />";
    exit();
}
// Display things to the page so you can see what is happening for testing purposes
echo "The file named <strong>$fileName</strong> uploaded successfuly.<br /><br />";
echo "It is <strong>$fileSize</strong> bytes in size.<br /><br />";
echo "It is a <strong>$fileType</strong> type of file.<br /><br />";
echo "The Error Message output for this upload is: <br />$fileErrorMsg";
?>

When I do that, I get this error when going to the site:

 

"ERROR: Please browse for a file before clicking the upload button."

 

I guess this is because it "runs" the whole index.php whenever you open the site, resulting in an error because the upload script is also being run - obviously without a file choosen.

 

Any way around this? without disabling the messages

 

This line

<form enctype="multipart/form-data" method="post" action="file_upload_script.php">

Basically tell the script to go to file_upload_script.php when you click submit. So if you remove action="file_upload_script.php", it will stay in index.php.

When I do that, I get this error when going to the site:

 

"ERROR: Please browse for a file before clicking the upload button."

 

I guess this is because it "runs" the whole index.php whenever you open the site, resulting in an error because the upload script is also being run - obviously without a file choosen.

 

Any way around this? without disabling the messages

 

This line

<form enctype="multipart/form-data" method="post" action="file_upload_script.php">

Basically tell the script to go to file_upload_script.php when you click submit. So if you remove action="file_upload_script.php", it will stay in index.php.

 

What you need to do is add an extra variable to the form as a hidden variable, call it form_submitted and then check for that variable before including file_upload_script.php. That way you won't get the error message if you haven't submitted the forum.

 

<!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-Language" content="da" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
.style1 {
text-align: left;
}
</style>
</head>

<body>
<p>UPLOAD FILE</p>
<form enctype="multipart/form-data" method="post" action="file_upload_script.php">
<div class="style1">
Choose file:<br />
<input name="file1" type="file" /><br />
<input type="submit" value="Go" />
<input type="reset" value="Reset" />
<input type="hidden" name="form_submitted" value="1" />
</div>
</form>
</body>
<?php if(isset($_POST['form_submitted'])) include("file_upload_script.php"); ?>

When I do that, I get this error when going to the site:

 

"ERROR: Please browse for a file before clicking the upload button."

 

I guess this is because it "runs" the whole index.php whenever you open the site, resulting in an error because the upload script is also being run - obviously without a file choosen.

 

Any way around this? without disabling the messages

 

This line

<form enctype="multipart/form-data" method="post" action="file_upload_script.php">

Basically tell the script to go to file_upload_script.php when you click submit. So if you remove action="file_upload_script.php", it will stay in index.php.

 

What you need to do is add an extra variable to the form as a hidden variable, call it form_submitted and then check for that variable before including file_upload_script.php. That way you won't get the error message if you haven't submitted the forum.

 

<!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-Language" content="da" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
.style1 {
text-align: left;
}
</style>
</head>

<body>
<p>UPLOAD FILE</p>
<form enctype="multipart/form-data" method="post" action="file_upload_script.php">
<div class="style1">
Choose file:<br />
<input name="file1" type="file" /><br />
<input type="submit" value="Go" />
<input type="reset" value="Reset" />
<input type="hidden" name="form_submitted" value="1" />
</div>
</form>
</body>
<?php if(isset($_POST['form_submitted'])) include("file_upload_script.php"); ?>

 

 

But then I have to include the file_upload_script.php file, and then the output doesn't show on the same page as the upload form. Or am I missing something?

try this bady i have remove the innitial comment so you can be able to see my comments cleary and understand what i have done. you can also cut the whole php code from your form page and create anew one so then you call it as afunction or include. thanx

 

<!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-Language" content="da" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css">
    .style1 {

    text-align: left;
    }
    </style>
    </head>
<?php

$fileName = $_FILES["file1"]["name"]; 
$fileTmpLoc = $_FILES["file1"]["tmp_name"]; 
$fileType = $_FILES["file1"]["type"]; 
$fileSize = $_FILES["file1"]["size"]; 
$fileErrorMsg = $_FILES["file1"]["error"]; 

// First make sure that the vistor is visting the page for the first time or he has pressed the submit batton.
if(isset($_POST['Go'])){ // this will make sure that it only runs this code only when the batton go is pressed.

	// now lets set our erro masseges into a variable that can only be called once anywhere we like it with $massege.

	if (!$fileTmpLoc) { 
		$massege = "ERROR: Please browse for a file before clicking the upload button.";

	} else if($fileSize > 524288000) { 
		$massege = "ERROR: File size restriction. File larger than 500mb.";
		unlink($fileTmpLoc);

	}
	if(!$massege){
	move_uploaded_file($fileTmpLoc, "test/$fileName");

	if (!file_exists("test/$fileName")) {
		$massege = "ERROR: File not uploaded. $filename alreadt exists.<br /><br />";

	}		
	$massege = "The file named <strong>$fileName</strong> uploaded successfuly.<br /><br />";
	$massege .= "It is <strong>$fileSize</strong> bytes in size.<br /><br />";
	$massege .=  "It is a <strong>$fileType</strong> type of file.<br /><br />";
	$massege .=  "The Error Message output for this upload is: <br />$fileErrorMsg";
	echo $massege;
	}else{
	echo $massege;
	}
}
?>


    <body>
    <p>UPLOAD FILE</p>
    <!--  i have used a $_SERVER['PHP_SELF'] because i want the form to submit on its  self-->
    <form enctype="multipart/form-data" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
    <div class="style1">

    	

    Choose file:<br />
    <input name="file1" type="file" /><br />
     <!--  i have added a Go as a named attribute on the submit batton so i can be abble to track if a user has pressed the submit batton or he is just browsing so that my cde can not be ran where it not needed-->
    <input type="submit" name="Go" value="Go" />
    <input type="reset" value="Reset" />
    </div>
    </form>
    </body>


If you include the file anything in that file will be outputted on index.php.

 

It doesn't output the messages etc on index.php, but on a blank site. And the url changes to domain.dk/upload_script.php

 

try this bady i have remove the innitial comment so you can be able to see my comments cleary and understand what i have done. you can also cut the whole php code from your form page and create anew one so then you call it as afunction or include. thanx

 

<!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-Language" content="da" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css">
    .style1 {

    text-align: left;
    }
    </style>
    </head>
<?php

$fileName = $_FILES["file1"]["name"]; 
$fileTmpLoc = $_FILES["file1"]["tmp_name"]; 
$fileType = $_FILES["file1"]["type"]; 
$fileSize = $_FILES["file1"]["size"]; 
$fileErrorMsg = $_FILES["file1"]["error"]; 

// First make sure that the vistor is visting the page for the first time or he has pressed the submit batton.
if(isset($_POST['Go'])){ // this will make sure that it only runs this code only when the batton go is pressed.

	// now lets set our erro masseges into a variable that can only be called once anywhere we like it with $massege.

	if (!$fileTmpLoc) { 
		$massege = "ERROR: Please browse for a file before clicking the upload button.";

	} else if($fileSize > 524288000) { 
		$massege = "ERROR: File size restriction. File larger than 500mb.";
		unlink($fileTmpLoc);

	}
	if(!$massege){
	move_uploaded_file($fileTmpLoc, "test/$fileName");

	if (!file_exists("test/$fileName")) {
		$massege = "ERROR: File not uploaded. $filename alreadt exists.<br /><br />";

	}		
	$massege = "The file named <strong>$fileName</strong> uploaded successfuly.<br /><br />";
	$massege .= "It is <strong>$fileSize</strong> bytes in size.<br /><br />";
	$massege .=  "It is a <strong>$fileType</strong> type of file.<br /><br />";
	$massege .=  "The Error Message output for this upload is: <br />$fileErrorMsg";
	echo $massege;
	}else{
	echo $massege;
	}
}
?>


    <body>
    <p>UPLOAD FILE</p>
    <!--  i have used a $_SERVER['PHP_SELF'] because i want the form to submit on its  self-->
    <form enctype="multipart/form-data" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
    <div class="style1">

    	

    Choose file:<br />
    <input name="file1" type="file" /><br />
     <!--  i have added a Go as a named attribute on the submit batton so i can be abble to track if a user has pressed the submit batton or he is just browsing so that my cde can not be ran where it not needed-->
    <input type="submit" name="Go" value="Go" />
    <input type="reset" value="Reset" />
    </div>
    </form>
    </body>


 

That doesn't work. The error message doesn't show up anymore, but now it doesn't upload anything, even though it shows the 'upload success'-message.

Wow, this advice is all over the place.  Lets keep it simple, and keep running with the code you have.

There are NO changes to be made in file_upload_script.php.

 

index.php Note*: form action removed, other changes at bottom of the script (commented).

<!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-Language" content="da" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
.style1 {



text-align: left;
}
</style>
</head>

<body>
<p>UPLOAD FILE</p>
<form enctype="multipart/form-data" method="post" >
<div class="style1">



Choose file:<br />
<input name="file1" type="file" /><br />
<input type="submit" value="Go" />
<input type="reset" value="Reset" />
</div>
</form>
</body>
<?php 
if($_SERVER['REQUEST_METHOD'] == 'POST') { //check to see if the form was submitted.
include("file_upload_script.php"); //include the upload script if the form was submitted.
} //ends the if statement.
?>

Here is you something to play around with.

 

This will let you choose a directory, or make a new one.

 

index.php

<?php
//get directories for images.

/******NOTE (point this variable to your image directory, it will find all subfolders) *************/
$image_directory = 'test'; //Where do you want to look for image directories at?
/********************************************************************************************************/

foreach (glob($image_directory . DIRECTORY_SEPARATOR . "*", GLOB_ONLYDIR) as $dir) {   //this will find ONLY directories.
    $directories[] = substr($dir,(strpos($dir,DIRECTORY_SEPARATOR) + 1));   //$directories array will only be set IF there are directories inside of the specified $image_directory.
}
?>
<!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-Language" content="da" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
.style1 {



text-align: left;
}
</style>
<script type="text/javascript">
function newInput() {
var place = document.getElementById('dir').innerHTML = '<br /><label for="dir">New Directory</label><br /><input name="dir" type="text" /><br />';
}
</script>
</head>

<body>
<p>UPLOAD FILE</p>
<form enctype="multipart/form-data" method="post" >
<div class="style1">



Choose file:<br />
<input name="file1" type="file" /><br />
Choose Directory: <span style="font-size: .83em; line-height: 0.5em; vertical-align: baseline; position: relative; top: -0.4em;">(Leave blank for base directory.)</span>
<br />
<?php echo $image_directory . DIRECTORY_SEPARATOR; ?>
<span id="dir">
<select name="dir">
<option></option>
<?php 
if(!empty($directories)) { 
foreach($directories as $folder) { 
	echo '<option>' . $folder . '</option>' . PHP_EOL;
}
}
?>
</select><br />
<a href="javascript:void(0);" onclick="newInput();">Add a Directory?</a>
</span><br />
<input type="submit" value="Go" />
<input type="reset" value="Reset" />
</div>
</form>
</body>
<?php 
if($_SERVER['REQUEST_METHOD'] == 'POST') { //check to see if the form was submitted.
//include("file_upload_script.php"); //include the upload script if the form was submitted.
include('test4.php');
} //ends the if statement.
?>

 

file_upload_script.php

<?php
// Set local PHP vars from the POST vars sent from our form using the array
// of data that the $_FILES global variable contains for this uploaded file
$fileName = $_FILES["file1"]["name"]; // The file name
$fileTmpLoc = $_FILES["file1"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["file1"]["type"]; // The type of file it is
$fileSize = $_FILES["file1"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["file1"]["error"]; // 0 for false... and 1 for true
$directory = (empty($_POST['dir'])) ? NULL : DIRECTORY_SEPARATOR . strip_tags($_POST['dir']); //if the form dir is empty, then make this variable null, otherwise set it as the directory.
if($directory != NULL && !is_dir($image_directory . $directory)) { //if $directory is NOT null, and it IS NOT a directory,
if(!mkdir($image_directory . $directory, 0775)) { //try to make the directory, if it fails to make.
	echo "ERROR: Unable to create directory, please make $image_directory is readable and writeable, and the owner has execute permission."; //throw an error, AND
	exit(); //stop the script.  No file upload.
}
}
$path = $image_directory . $directory . DIRECTORY_SEPARATOR . $fileName; //if everything has passed, make the path. (if directory was empty, the path will be to the main $image_directory.
// Specific Error Handling if you need to run error checking
if (!$fileTmpLoc) { // if file not chosen
    echo "ERROR: Please browse for a file before clicking the upload button.";
    exit();
} else if($fileSize > 524288000) { // if file is larger than we want to allow
    echo "ERROR: File size restriction. File larger than 500mb.";
    unlink($fileTmpLoc);
    exit();
}
// Place it into your "uploads" folder mow using the move_uploaded_file() function 
move_uploaded_file($fileTmpLoc, "$path");
// Check to make sure the uploaded file is in place where you want it
if (!file_exists("$path")) {
    echo "ERROR: File not uploaded. $fileName alreadt exists.<br /><br />";
    exit();
}
// Display things to the page so you can see what is happening for testing purposes
echo "The file named <strong>$fileName</strong> uploaded successfuly.<br /><br />" 
	. "The file path is " . $path . '<br /><br />';
echo "It is <strong>$fileSize</strong> bytes in size.<br /><br />";
echo "It is a <strong>$fileType</strong> type of file.<br /><br />";
echo "The Error Message output for this upload is: <br />$fileErrorMsg";
?>

If you include the file anything in that file will be outputted on index.php.

 

It doesn't output the messages etc on index.php, but on a blank site. And the url changes to domain.dk/upload_script.php

 

try this bady i have remove the innitial comment so you can be able to see my comments cleary and understand what i have done. you can also cut the whole php code from your form page and create anew one so then you call it as afunction or include. thanx

 

<!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-Language" content="da" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css">
    .style1 {

    text-align: left;
    }
    </style>
    </head>
<?php

$fileName = $_FILES["file1"]["name"]; 
$fileTmpLoc = $_FILES["file1"]["tmp_name"]; 
$fileType = $_FILES["file1"]["type"]; 
$fileSize = $_FILES["file1"]["size"]; 
$fileErrorMsg = $_FILES["file1"]["error"]; 

// First make sure that the vistor is visting the page for the first time or he has pressed the submit batton.
if(isset($_POST['Go'])){ // this will make sure that it only runs this code only when the batton go is pressed.

	// now lets set our erro masseges into a variable that can only be called once anywhere we like it with $massege.

	if (!$fileTmpLoc) { 
		$massege = "ERROR: Please browse for a file before clicking the upload button.";

	} else if($fileSize > 524288000) { 
		$massege = "ERROR: File size restriction. File larger than 500mb.";
		unlink($fileTmpLoc);

	}
	if(!$massege){
	move_uploaded_file($fileTmpLoc, "test/$fileName");

	if (!file_exists("test/$fileName")) {
		$massege = "ERROR: File not uploaded. $filename alreadt exists.<br /><br />";

	}		
	$massege = "The file named <strong>$fileName</strong> uploaded successfuly.<br /><br />";
	$massege .= "It is <strong>$fileSize</strong> bytes in size.<br /><br />";
	$massege .=  "It is a <strong>$fileType</strong> type of file.<br /><br />";
	$massege .=  "The Error Message output for this upload is: <br />$fileErrorMsg";
	echo $massege;
	}else{
	echo $massege;
	}
}
?>


    <body>
    <p>UPLOAD FILE</p>
    <!--  i have used a $_SERVER['PHP_SELF'] because i want the form to submit on its  self-->
    <form enctype="multipart/form-data" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
    <div class="style1">

    	

    Choose file:<br />
    <input name="file1" type="file" /><br />
     <!--  i have added a Go as a named attribute on the submit batton so i can be abble to track if a user has pressed the submit batton or he is just browsing so that my cde can not be ran where it not needed-->
    <input type="submit" name="Go" value="Go" />
    <input type="reset" value="Reset" />
    </div>
    </form>
    </body>


 

That doesn't work. The error message doesn't show up anymore, but now it doesn't upload anything, even though it shows the 'upload success'-message.

 

i tested it several times and it was working, make sure you have the test folder in your root directory.

Here is you something to play around with.

 

This will let you choose a directory, or make a new one.

 

index.php

<?php
//get directories for images.

/******NOTE (point this variable to your image directory, it will find all subfolders) *************/
$image_directory = 'test'; //Where do you want to look for image directories at?
/********************************************************************************************************/

foreach (glob($image_directory . DIRECTORY_SEPARATOR . "*", GLOB_ONLYDIR) as $dir) {   //this will find ONLY directories.
    $directories[] = substr($dir,(strpos($dir,DIRECTORY_SEPARATOR) + 1));   //$directories array will only be set IF there are directories inside of the specified $image_directory.
}
?>
<!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-Language" content="da" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
.style1 {



text-align: left;
}
</style>
<script type="text/javascript">
function newInput() {
var place = document.getElementById('dir').innerHTML = '<br /><label for="dir">New Directory</label><br /><input name="dir" type="text" /><br />';
}
</script>
</head>

<body>
<p>UPLOAD FILE</p>
<form enctype="multipart/form-data" method="post" >
<div class="style1">



Choose file:<br />
<input name="file1" type="file" /><br />
Choose Directory: <span style="font-size: .83em; line-height: 0.5em; vertical-align: baseline; position: relative; top: -0.4em;">(Leave blank for base directory.)</span>
<br />
<?php echo $image_directory . DIRECTORY_SEPARATOR; ?>
<span id="dir">
<select name="dir">
<option></option>
<?php 
if(!empty($directories)) { 
foreach($directories as $folder) { 
	echo '<option>' . $folder . '</option>' . PHP_EOL;
}
}
?>
</select><br />
<a href="javascript:void(0);" onclick="newInput();">Add a Directory?</a>
</span><br />
<input type="submit" value="Go" />
<input type="reset" value="Reset" />
</div>
</form>
</body>
<?php 
if($_SERVER['REQUEST_METHOD'] == 'POST') { //check to see if the form was submitted.
//include("file_upload_script.php"); //include the upload script if the form was submitted.
include('test4.php');
} //ends the if statement.
?>

 

file_upload_script.php

<?php
// Set local PHP vars from the POST vars sent from our form using the array
// of data that the $_FILES global variable contains for this uploaded file
$fileName = $_FILES["file1"]["name"]; // The file name
$fileTmpLoc = $_FILES["file1"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["file1"]["type"]; // The type of file it is
$fileSize = $_FILES["file1"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["file1"]["error"]; // 0 for false... and 1 for true
$directory = (empty($_POST['dir'])) ? NULL : DIRECTORY_SEPARATOR . strip_tags($_POST['dir']); //if the form dir is empty, then make this variable null, otherwise set it as the directory.
if($directory != NULL && !is_dir($image_directory . $directory)) { //if $directory is NOT null, and it IS NOT a directory,
if(!mkdir($image_directory . $directory, 0775)) { //try to make the directory, if it fails to make.
	echo "ERROR: Unable to create directory, please make $image_directory is readable and writeable, and the owner has execute permission."; //throw an error, AND
	exit(); //stop the script.  No file upload.
}
}
$path = $image_directory . $directory . DIRECTORY_SEPARATOR . $fileName; //if everything has passed, make the path. (if directory was empty, the path will be to the main $image_directory.
// Specific Error Handling if you need to run error checking
if (!$fileTmpLoc) { // if file not chosen
    echo "ERROR: Please browse for a file before clicking the upload button.";
    exit();
} else if($fileSize > 524288000) { // if file is larger than we want to allow
    echo "ERROR: File size restriction. File larger than 500mb.";
    unlink($fileTmpLoc);
    exit();
}
// Place it into your "uploads" folder mow using the move_uploaded_file() function 
move_uploaded_file($fileTmpLoc, "$path");
// Check to make sure the uploaded file is in place where you want it
if (!file_exists("$path")) {
    echo "ERROR: File not uploaded. $fileName alreadt exists.<br /><br />";
    exit();
}
// Display things to the page so you can see what is happening for testing purposes
echo "The file named <strong>$fileName</strong> uploaded successfuly.<br /><br />" 
	. "The file path is " . $path . '<br /><br />';
echo "It is <strong>$fileSize</strong> bytes in size.<br /><br />";
echo "It is a <strong>$fileType</strong> type of file.<br /><br />";
echo "The Error Message output for this upload is: <br />$fileErrorMsg";
?>

 

Wow, this advice is all over the place.  Lets keep it simple, and keep running with the code you have.

There are NO changes to be made in file_upload_script.php.

 

I just really appreciate that people are trying to help :-) Thats what makes me donate to sites like this.

 

Anyway, thats seems really good and is just what I need, but i can't get it to work. When trying to upload or create a folder, it displays the following errors:

 

Warning: include(test4.php) [function.include]: failed to open stream: No such file or directory in /var/www/html/shweb84/sec/index.php on line 66

Warning: include() [function.include]: Failed opening 'test4.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/html/shweb84/sec/index.php on line 66

 

Line 66 in index.php includes test4.php which I don't have. I guess it's just something you used while making the script? I tried to comment it out but then the script doesn't work at all - doesn't upload or create folders and doesn't show any errors.

 

Am I doing something wrong?

You'll notice the line above it says:

//include("file_upload_script.php"); //include the upload script if the form was submitted.

 

You need to un-comment it like:

include("file_upload_script.php"); //include the upload script if the form was submitted.

 

And remove the test4.php include.  My mistake, thought I removed that.

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.