Jump to content

Php script and form in the same page?


monkeyhead

Recommended Posts

Hi there,

 

I'm just working on a basic up-loader at the moment and I was wondering how to implement the script and form into the same file?

 

I've tried the following below but it shows my error messages that I didn't specify the images , however the form still uploads.

 

 

What I want to do is show the form and process it on the same page. I think its possible?

 

 

<?php


function imgprocess($f_name){

$max_size = 6291456;  //Set the max image size in bytes default is approx 6MB

$target_path = "uploads/"; //The path where the images are uploaded

$target_path = $target_path . basename( $_FILES[$f_name]['name']); 

$type=$_FILES[$f_name]['type'];
$size=$_FILES[$f_name]['size'];




if ($size>$max_size)
{
echo "ERROR <br> the image size is too big<br><br>";
}




if ($size<$max_size )
{
if(move_uploaded_file($_FILES[$f_name]['tmp_name'], $target_path)) {
    

echo("<table>");



echo "<tr><td><h1>However.....</h1>The file ".  basename( $_FILES[$f_name]['name']). 
    " has been uploaded</tr></td>";


echo '<tr><td><img src="' . $target_path . '" width="128" height="128"/></td></tr>';


echo "<td><ul>Img title:". basename( $_FILES[$f_name]['name']). "<br>";

echo "Img size:". basename( $_FILES[$f_name]['size']). " bytes<br>";

echo "File type:". basename( $_FILES[$f_name]['type']). "<br>";

echo("<a href=\"$target_path\"> Heres a link to your uploaded image!</a><br></td></tr>");


} else{
    echo "Either you didnt specifiy an image for  $f_name to upload or there was an error uploading the file, please try again!<br><br><br><br><br>";
}

}

}

imgprocess(img1);
imgprocess(img2);
imgprocess(img3);
imgprocess(img4);



?>





<html><head><title> MJSEY image uploader</title> </head>

<body><h3> MJSEY image uploader </h3>


<img src="nt.jpg" alt="Angry face" width="128" height="128" align="middle" />




Please select the desired images you would like to upload:</br>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data">

<input type="file" name="img1" size="50">
</br>
<input type="file" name="img2" size="50">
</br>
<input type="file" name="img3" size="50">
</br>
<input type="file" name="img4" size="50">
</br>
<input type="submit" value="Submit!">
</form>

</body></html>

Link to comment
Share on other sites

In addition to what phpSensei mentioned, I assume you mean you're getting the errors on the initial page load, correct?

 

Correct.

 

Also this is where I'm getting the variables from

 <input type="file" name="img1" size="50">
</br>
<input type="file" name="img2" size="50">
</br>
<input type="file" name="img3" size="50">
</br>
<input type="file" name="img4" size="50">

 

Link to comment
Share on other sites

In addition to what phpSensei mentioned, I assume you mean you're getting the errors on the initial page load, correct?

 

Correct.

 

Also this is where I'm getting the variables from

 <input type="file" name="img1" size="50">
</br>
<input type="file" name="img2" size="50">
</br>
<input type="file" name="img3" size="50">
</br>
<input type="file" name="img4" size="50">

 

Those aren't variables. Those are the name of your input fields... :P

 

$img1 = $_POST['img1']['name'];

imgprocess($img1);

 

 

Link to comment
Share on other sites

In addition to what phpSensei mentioned, I assume you mean you're getting the errors on the initial page load, correct?

 

 

 

Correct.

 

Also this is where I'm getting the variables from

 <input type="file" name="img1" size="50">
</br>
<input type="file" name="img2" size="50">
</br>
<input type="file" name="img3" size="50">
</br>
<input type="file" name="img4" size="50">

 

Those aren't variables. Those are the name of your input fields... :P

 

$img1 = $_POST['img1']['name'];

imgprocess($img1);

 

 

 

I tried that but still have the same issue :(

Link to comment
Share on other sites

try this

 

<?php


function imgprocess($f_name){

$max_size = 6291456;  //Set the max image size in bytes default is approx 6MB
$target_path = "uploads/"; //The path where the images are uploaded
$target_path = $target_path . basename( $_FILES[$f_name]['name']); 
$type=$_FILES[$f_name]['type'];
$size=$_FILES[$f_name]['size'];

if ($size>$max_size) {
   echo "ERROR <br> the image size is too big<br><br>";
}

	if ($size<$max_size )
	{
	if(move_uploaded_file($_FILES[$f_name]['tmp_name'], $target_path)) {
    

	echo "<table>";
	echo "<tr><td><h1>However.....</h1>The file ".basename( $_FILES[$f_name]['name'])." has been uploaded</tr></td>";
	echo '<tr><td><img src="' . $target_path . '" width="128" height="128"/></td></tr>';
	echo "<td><ul>Img title:". basename( $_FILES[$f_name]['name']). "<br>";
	echo "Img size:". basename( $_FILES[$f_name]['size']). " bytes<br>";
	echo "File type:". basename( $_FILES[$f_name]['type']). "<br>";
	echo("<a href=\"$target_path\"> Heres a link to your uploaded image!</a><br></td></tr>");


	}else{
	echo "Either you didnt specifiy an image for  $f_name to upload or there was an error uploading the file, please try again!<br><br>			 		<br><br><br>";
	}

}
}

if(isset($_POST['submit'])){

$image1 = $_FILES['img1'];
$image2 = $_FILES['img2'];
$image3 = $_FILES['img3'];
$image3 = $_FILES['img4'];


imgprocess($image1);
imgprocess($image2);
imgprocess($image3);
imgprocess($image4);

}

?>
<html>
<head>
<title> MJSEY image uploader</title> </head>
<body>
<h3> MJSEY image uploader </h3>
<img src="nt.jpg" alt="Angry face" width="128" height="128" align="middle" />

Please select the desired images you would like to upload:</br>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data">

<input type="file" name="img1" size="50">
</br>
<input type="file" name="img2" size="50">
</br>
<input type="file" name="img3" size="50">
</br>
<input type="file" name="img4" size="50">
</br>
<input type="submit" name ="submit" value="Submit!">
</form>
</body></html>

Link to comment
Share on other sites

try this

 

<?php


function imgprocess($f_name){

$max_size = 6291456;  //Set the max image size in bytes default is approx 6MB
$target_path = "uploads/"; //The path where the images are uploaded
$target_path = $target_path . basename( $_FILES[$f_name]['name']); 
$type=$_FILES[$f_name]['type'];
$size=$_FILES[$f_name]['size'];

if ($size>$max_size) {
   echo "ERROR <br> the image size is too big<br><br>";
}

	if ($size<$max_size )
	{
	if(move_uploaded_file($_FILES[$f_name]['tmp_name'], $target_path)) {
    

	echo "<table>";
	echo "<tr><td><h1>However.....</h1>The file ".basename( $_FILES[$f_name]['name'])." has been uploaded</tr></td>";
	echo '<tr><td><img src="' . $target_path . '" width="128" height="128"/></td></tr>';
	echo "<td><ul>Img title:". basename( $_FILES[$f_name]['name']). "<br>";
	echo "Img size:". basename( $_FILES[$f_name]['size']). " bytes<br>";
	echo "File type:". basename( $_FILES[$f_name]['type']). "<br>";
	echo("<a href=\"$target_path\"> Heres a link to your uploaded image!</a><br></td></tr>");


	}else{
	echo "Either you didnt specifiy an image for  $f_name to upload or there was an error uploading the file, please try again!<br><br>			 		<br><br><br>";
	}

}
}

if(isset($_POST['submit'])){

$image1 = $_FILES['img1'];
$image2 = $_FILES['img2'];
$image3 = $_FILES['img3'];
$image3 = $_FILES['img4'];


imgprocess($image1);
imgprocess($image2);
imgprocess($image3);
imgprocess($image4);

}

?>
<html>
<head>
<title> MJSEY image uploader</title> </head>
<body>
<h3> MJSEY image uploader </h3>
<img src="nt.jpg" alt="Angry face" width="128" height="128" align="middle" />

Please select the desired images you would like to upload:</br>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data">

<input type="file" name="img1" size="50">
</br>
<input type="file" name="img2" size="50">
</br>
<input type="file" name="img3" size="50">
</br>
<input type="file" name="img4" size="50">
</br>
<input type="submit" name ="submit" value="Submit!">
</form>
</body></html>

 

That worked, however i now get a new error :

 

Warning: Illegal offset type in /img/index.php on line 8

Warning: Illegal offset type in /img/index.php on line 9

Warning: Illegal offset type in /img/index.php on line 10

Link to comment
Share on other sites

did it upload the images?

 

In this case I would ignore this.. It has to do with the array keys actually..

Array s and object s can not be used as keys. Doing so will result in a warning: Illegal offset type. 

 

Anyone correct me if i'm wrong, still a little rusty with php here since I havnt been coding for a few months.

 

I believe its just the way your going about uploading this thing. A much easier way of doing this would have been to just name each input file field as "img[]" instead of "img1,img2,img..etc"..

 

But this error isn't a problem really, you can do a if(isset()) for the array but thats a pain in the ass. You can also do error_reporting(0) so users can see it...

 

 

Link to comment
Share on other sites

It didnt upload either :(

 

AHH

 

$target_path = $target_path . basename( $_FILES[$f_name]['name']);

You see theres the problem

 

How was i so blind? $f_name doesnt go into $_FILES because thats like saying $_FILES[$_FILES['img1']]]['name']

 

You understand what i am saying?

 

 

Link to comment
Share on other sites

Here  yuo go champ :).... *erm* trust me I am still rusty with php, some obvious mistakes I dont always catch, I am getting good again though. And uhhh I was jus ttesting you, trying to see if you can find the mistake... lol just keeding

 

Disregard what I said about the Array Keys and Offset, that was the problem...

 

<?php

function imgprocess($f_name){

$max_size = 6291456;  //Set the max image size in bytes default is approx 6MB
$target_path = "uploads/"; //The path where the images are uploaded
$target_path = $target_path . basename( $f_name['name']); 
$type=$f_name['type'];
$size=$f_name['size'];

if ($size>$max_size) {
   echo "ERROR <br> the image size is too big<br><br>";
}

	if ($size<$max_size )
	{
	if(move_uploaded_file($f_name['tmp_name'], $target_path)) {
    

	echo "<table>";
	echo "<tr><td><h1>However.....</h1>The file ".basename( $f_name['name'])." has been uploaded</tr></td>";
	echo '<tr><td><img src="' . $target_path . '" width="128" height="128"/></td></tr>';
	echo "<td><ul>Img title:". basename( $f_name['name']). "<br>";
	echo "Img size:". basename($f_name['size']). " bytes<br>";
	echo "File type:". basename($f_name['type']). "<br>";
	echo("<a href=\"$target_path\"> Heres a link to your uploaded image!</a><br></td></tr>");


	}else{
	echo "Either you didnt specifiy an image for  ".$f_name['name']." to upload or there was an error uploading the file, please try again!<br><br>			 		<br><br><br>";
	}

}
}

if(isset($_POST['submit'])){

$image1 = $_FILES['img1'];
$image2 = $_FILES['img2'];
$image3 = $_FILES['img3'];
$image3 = $_FILES['img4'];

imgprocess($image1);
imgprocess($image2);
imgprocess($image3);
imgprocess($image4);


}

?>
<html>
<head>
<title> MJSEY image uploader</title> </head>
<body>
<h3> MJSEY image uploader </h3>
<img src="nt.jpg" alt="Angry face" width="128" height="128" align="middle" />

Please select the desired images you would like to upload:</br>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data">

<input type="file" name="img1" size="50">
</br>
<input type="file" name="img2" size="50">
</br>
<input type="file" name="img3" size="50">
</br>
<input type="file" name="img4" size="50">
</br>
<input type="submit" name ="submit" value="Submit!">
</form>
</body></html>

Link to comment
Share on other sites

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.