I am trying to do what I think is the simplest thing, however I do not understand what is wrong. I have an php file that has a form. The form accepts data from the user and writes it to a text file. This works perfectly. Also in this form, I would like the user to be able to upload a file to the server. Problem begins here.
With the upload script and form, if I create a test file and drop the code in it by itself, it works perfect and successfully uploads the file to the server. However, if I take the same code and put it in my other code, it will not upload the file. Is there a problem having a seperate form within a form or something? I just don't see why my code won't work, or what I need to do to fix it. Thanks in advance for your help.
Below is the code that works great when put in a file by itself.
<html>
<head></head>
<body>
<!--Upload file-->
<form enctype="multipart/form-data" action="<?php echo $_SERVER['SCRIPT_NAME']?>" method="POST">
Please choose image file for this movie: <br><input name="uploaded" type="file" /><br />
<input type="submit" name="upload" value="Upload" />
</form>
</p>
</html>
<?php
if (isset($_POST['upload'])) {
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
//This is our size condition
//if ($uploaded_size > 350000)
//{
//echo "Your file is too large.<br>";
//$ok=0;
//}
//This is our limit file type condition
if ($uploaded_type =="image/jpg")
{
echo "You may only upload JPG files.<br>";
$ok=0;
}
//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}
//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
}
?>
However, when I put this exact same code in my other file, it will not upload.
<?php
if (isset($_POST['submit'])) {
$weekDate = $_POST['weekDate'];
$title1 = $_POST['title-1'];
$rating1 = $_POST['rating-1'];
$actors1 = $_POST['actors-1'];
$image1 = $_POST['image-1'];
$imageLink1 = $_POST['imageLink-1'];
$description1 = $_POST['description-1'];
//remove \n
$description1 = str_replace("\n", "", $description1);
$fri1 = $_POST['fri-1'];
$sat1 = $_POST['sat-1'];
$sun1 = $_POST['sun-1'];
$mon1 = $_POST['mon-1'];
$tues1 = $_POST['tues-1'];
$wed1 = $_POST['wed-1'];
$thur1 = $_POST['thur-1'];
$fp = fopen("../db/data.txt","w");
if(!$fp) {
echo 'Error, the file could not be opened or there was an error creating it.';
exit;
}
fwrite($fp, $weekDate."\n" . $title1."\n" . $rating1."\n" . $actors1 . "\n" . $image1. "\n" . $imageLink1.
"\n" . $description1. "\n" . $fri1. "\n" . $sat1. "\n" . $sun1. "\n" . $mon1. "\n" . $tues1. "\n" . $wed1.
"\n" . $thur1);
fclose($fp);
header( 'Location: http://www.mydomain.com/success.html' ) ;
//echo 'Your data was saved successfully!';
}
?>
<?php
//THE SCRIPT IS HERE BUT IT WON"T UPLOAD SUCCESSFULLY.
if (isset($_POST['upload'])) {
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
//This is our size condition
//if ($uploaded_size > 350000)
//{
//echo "Your file is too large.<br>";
//$ok=0;
//}
//This is our limit file type condition
if ($uploaded_type =="image/jpg")
{
echo "You may only upload JPG files.<br>";
$ok=0;
}
//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}
//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file."; //IT RETURNS THIS ERROR MESSAGE
}
}
}
?>
<html>
<head></head>
<body>
<?php
// read file into array
$array = file('../db/data.txt');
?>
<center><b>UPDATE CURRENT SHOW INFORMATION </b> </center>
<hr>
<p>
<form name="input" method="post"
action="<?php echo $_SERVER['SCRIPT_NAME']?>">
</p><label for="date"><b>Movies and Times Showing for the Week Of:</b> </label>
<input type="text" size="60" name="weekDate" id="weekDate" value="<?php echo $array[0];?> ">
<br><br>
<hr>
<table>
<tr><td bgcolor="#99CCFF">
<center><h2>MOVIE 1</h2></center><br>
<p><label for="title-1"><b>Title:</b> </label>
<input type="text" size="60" name="title-1" id="title-1" value="<?php echo $array[1];?>"/>
<label for="rating-1"><b>Rating:</b> </label>
<input type="text" size="15" name="rating-1" id="rating-1" value="<?php echo $array[2];?>"/></p>
<p><label for="actors-1"><b>Main Actors:</b> </label>
<input type="text" size="70" name="actors-1" id="actors-1" value="<?php echo $array[3];?>"/></p>
<p><label for="image-1":><b>Name of Image:</b> </label>
<input type="text" size="30" name="image-1" id="image-1" value="<?php echo $array[4];?>"/></p>
<!--HERE IS FORM FOR UPLOAD-->
<p><form enctype="multipart/form-data" action="<?php echo $_SERVER['SCRIPT_NAME']?>" method="POST">
Please choose image file for this movie: <br><input name="uploaded" type="file" /><br />
<input type="submit" name="upload" value="Upload" />
</form>
</p>
<p><label for="imageLink-1"><b>Link to Image Trailer:</b> </label>
<input type="text" size="70" name="imageLink-1" id="imageLink-1" value="<?php echo $array[5];?>"/></p>
<p><b>Movie 1 Description:</b><br>
<textarea rows="10" cols="80" name="description-1"><?php echo $array[6];?></textarea></p>
<table>
<tr><td colspan="7" align="center"><b>Showtimes</b></td></tr>
<tr >
<td align="center">Friday</td>
<td align="center">Saturday</td>
<td align="center">Sunday</td>
<td align="center">Monday</td>
<td align="center">Tuesday</td>
<td align="center">Wednesday</td>
<td align="center" >Thursday</td>
</tr>
<tr>
<td ><input type="text" size="20" name="fri-1" id="fri-1" value="<?php echo $array[7];?>"/></td>
<td><input type="text" size="20" name="sat-1" id="sat-1" value="<?php echo $array[8];?>"/></td>
<td><input type="text" size="20" name="sun-1" id="sun-1" value="<?php echo $array[9];?>"/></td>
<td><input type="text" size="20" name="mon-1" id="mon-1" value="<?php echo $array[10];?>"/></td>
<td><input type="text" size="20" name="tues-1" id="tues-1" value="<?php echo $array[11];?>"/></td>
<td><input type="text" size="20" name="wed-1" id="wed-1" value="<?php echo $array[12];?>"/></td>
<td><input type="text" size="20" name="thur-1" id="thur-1" value="<?php echo $array[13];?>"/></td>
</tr>
</table>
</p>
<br>
</td></tr>
<tr><td>
<center><input type="submit" name="submit" value="Save Changes" /></center></p>
</form>
</td></tr>
</table>
</html>