PHP Form seems to change action to a different script - php

I'm building a website that has a form that allows users to upload files, but the form seems to be going to the wrong place. I told it to go to "upload.php", but instead it tries to go to "“upload.php", which I did not include in the form action. It doesn't look like I added any unicode characters in the script. Any reason why this might be happening?
Here's my form in index.html:
https://pastebin.com/7PETk5Sy
<!DOCTYPE html>
<html>
<head>
<title>Deeper</title>
<link type="text/css" rel="stylesheet" href="style.css"/>
</head>
<body>
<div id="titlebar">
<img src="DeeperNetIcon.jpg"></img>
<h1>DeeperNet</h1>
Back to Deeper Homepage
<br/><br/>
<form action="view.php" method="post">Search for World by ID: <input type="text" name="world"> <input type="submit" value="Search"></form>
<br/>
</div>
<p color="white">Welcome to DeeperNet!</p>
<div id="upload">
<h1>Upload World to DeeperNet</h1>
<form action=“upload.php” method="post" enctype="multipart/form-data">
World File:
<br/><br/>
<input type="file" name="world">
<br/><br/>
World Name:
<br/>
<input type="text" name="name">
<br/>
Description:
<br/>
<input type="textbox" name="desc">
<br/><br/>
<input type="submit" value="Upload World">
</form>
<br/>
</div>
</body>
The index.html File has 2 Forms, the second form is the one I'm talking about.
Here's my upload.php Script:
https://pastebin.com/0FyuAX3Q
<?php
include("index.html");
if(!empty($_POST)) {
if(isset($_FILES["world"])) {
$world = $_FILES["world"];
$name = $world["name"];
$tmp_name = $world["tmp_name"];
$size = $world["size"];
$ext = explode('.', $name);
$ext = strtolower(end($ext));
$allowed = "deep";
if ($ext == $allowed) {
if ($size <= 300000) {
$id = uniqid('', true);
$destination = 'worlds/' . $id . '.' . $ext;
if(move_uploaded_file($tmp_name, $destination)) {
echo '<br/>World: "' . $name . '" uploaded to DeeperNet!';
echo “<br/>World ID: ” . $id;
$info = fopen('worlds/' . $id . '.txt', 'w');
fwrite($info, $_POST['name'] . "\r\n");
fwrite($info, $_POST['desc']);
fclose($info);
}
}
}
MAMP says I'm using PHP 7.0.8 if you're wondering.

Replace your code with this as form accepts this double quotes (") not this one (“)
<form action="upload.php" method="post" enctype="multipart/form-data">

Related

FILE INPUT INSERT INTO BD WITH MOVE_TO_FILE

This is more of a question to gain a more clear understanding. If I insert from a form like:
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
into the DB for a link and it's successful:
$file = "INSERT INTO ('foo') VALUES ('FOO', NOW())";
just an example:
yet in the php script:
$_FILE['fileToUpload']['name'];
$_FILE['fileToUpload']['tmp_name'];
since the tmp_name folder only holds upload files in an array, which than I would have to use either a foreach or for loop search the loop of files, this than makes the INSERT INTO db difficult.
Question is how can I separate the search results from the array and than insert each one into the database?
HERE"S THE CODE:
<?php
$con = mysqli_connect("localhost","root","","acc1");
if(mysqli_connect_errno()){
echo 'Failed to connect to MySQL:' . mysqli_connect_error();
}else{
echo 'Connected!';
}
if(isset($_POST['submit']) && !empty($_FILES['fileBC']['name'] && !empty($_FILES['fileB']['name'] && !empty($_FILES['fileBR']['name']) ))){
$file = "image/";
$name = $_FILES['fileBC']['name'];
$data = $_FILES['fileBC']['tmp_name'];
$fileV = "video/";
$nameV = $_FILES['fileBR']['name'];
$dataV = $_FILES['fileBR']['tmp_name'];
$fileB = "book/";
$nameB = $_FILES['fileB']['name'];
$dataB = $_FILES['fileB']['tmp_name'];
if(move_uploaded_file($data,$file.$name)){
$ins_name = $con->query("INSERT INTO fileimages (fileBC, fileBR, fileB) VALUES ('$name','$nameB', '$nameV')");
}if($ins_name){
echo 'success!';
}else{
echo 'error';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/fontawesome.css">
<script src="js/jquery.js"></script>
<script src="js/bootstrap.js"></script>
</head>
<script>
function mymodal(){
$('#myModal').modal('show');
}
</script>
<body>
<form method="post" action="index.php" enctype="multipart/form-data">
<div class="form-group">
<label class="text-primary">Book Cover:</label>
<input class="form-control" type="file" name="fileBC" accept="image/*" >
</div>
<div class="form-group">
<label class="text-primary">Book:</label>
<input class="form-control" type="file" name="fileB" accept=".epub, .mobi, .pdf, .prc, .azw, .bbeb" >
</div>
<div class="form-group">
<label class="text-primary">Book Reading:</label>
<input class="form-control" type="file" name="fileBR" accept="video/*" >
</div>
<button name="submit">upload</button>
</form>
<p></p>
<ol>
</ol>
</body>
</html>
This is an example you can adept it as per your need.
<form action="file_reciever.php" enctype="multipart/form-data" method="post">
<input type="file" name="files[]" multiple/>
<input type="submit" name="submission" value="Upload"/>
</form>
PHP code is like
<?php
if (isset($_POST['submission'] && $_POST['submission'] != null) {
for ($i = 0; $i < count($_FILES['files']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['files']['tmp_name'][$i];
//Make sure we have a filepath
if ($tmpFilePath != "") {
//Setup our new file path
$newFilePath = "./uploadFiles/" . $_FILES['files']['name'][$i];
//Upload the file into the temp dir
if (move_uploaded_file($tmpFilePath, $newFilePath)) {
//Your insert statement goes here
}
}
}
}
?>

PHP Show image on the browser

test.php
<html>
<head></head>
<body>
<form action="" method="post" enctype="multipart/form-data">
Attachment: <input type="file" name="file" /><br />
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
move_uploaded_file($_FILES['file']['tmp_name'], "uploads/".$_FILES['file']['name']);
echo ''.$name.'';
}
?>
From the above code, how do I show the images that I uploaded on the showfile.php?
You'd also need to pass the uploaded file path to showfile.php unless you have a pattern somehow. Say you decided to use $_GET, the link in your code would be
echo '<a href="showfile.php?file=uploads/' . $_FILES['file']['name']
. ' target="_blank">' . $name . '</a>';
In showfile.php you'd have something like this:
$file = $_GET['file'];
echo '<img src="uploads/' . $file . '" />';

Uploaded images not in image folder

I just made a file upload code and I was wondering why the file upload wasn't working? I checked the permission of my image folder in localhost and that seems to be ok. I don't know where the problem exactly is. Any ideas?
<?php
require "config.php";
require "functions.php";
require "database.php";
if(isset($_FILES['fupload'])){
$filename = addslashes($_FILES['fupload']['name']);
$source = $_FILES['fupload']['tmp_name'];
$target = $path_to_image_directory . $filename;
$description = addslashes($_POST['description']);
$src = $path_to_image_directory . $filename;
$tn_src = $path_to_thumbs_directory . $filename;
if (strlen($_POST['description'])<4)
$error['description'] = '<p class="alert">Please enter a description for your photo</p>';
if($filename == '' || !preg_match('/[.](jpg)|(gif)|(png)|(jpeg)$/', $filename))
$error['no_file'] = '<p class="alert">Please select an image, dummy! </p>';
if (!isset($error)){
move_uploaded_file($source, $target);
$q = "INSERT into photo(description, src, tn_src)VALUES('$description', '$src','$tn_src')";
$result = $mysqli->query($q) or die (mysqli_error($myqli));
if ($result) {
echo "Succes! Your file has been uploaded";
}
createThumbnail($filename);
}
}
?><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Upload</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<h1>My photos</h1>
<ul><?php getPhotos(); ?></ul>
<h2>Upload a photo</h2>
<form enctype="multipart/form-data" action="index.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<input type="file" name="fupload" /><br/>
<textarea name="description" id="description" cols="50" rows="6"></textarea><br/>
<input type="submit" value="Upload photo" name="submit" />
</form>
<?php
if (isset($error["description"])) {
echo $error["description"];
}
if (isset($error["no_file"])) {
echo $error["no_file"];
}
?>
</body>
</html>
Please make sure that you appended directory separator in $path_to_thumbs_directory.
And you don't need to use addslashes to $filename.

php file upload in Jquery Mobile

Apologies if this is a duplicate question it just seems that every article i visit doesnt work for me.
I have a basic form which has two fields and a file.
<form action="make_announce.php" method="post" enctype="multipart/form-data" data-ajax="false">
<label>Enter Subject Line (500char max):
<input name="subject" type="text" id="subject" size="50"/></label>
<label>Announcement :
<textarea name="announce" cols="50" rows="10" id="announce"></textarea></label>
<label>Post Image (Leave Blank for NONE)<br>
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<input type="file" name="image" /><br /></label>
<div align="center">
<input type="submit" name="Submit" value="OK" />
</div>
</form>
And my PHP File minus most of the variables.
if (!empty($_FILES["image"])) {
$myFile = $_FILES["image"];
if ($myFile["error"] !== UPLOAD_ERR_OK) {
$sql="...
mysqli_query($con,$sql);
exit;
}
$name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);
$i = 0;
$parts = pathinfo($name);
while (file_exists(UPLOAD_DIR . $name)) {
$i++;
$name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
}
$success = move_uploaded_file($myFile["tmp_name"],
UPLOAD_DIR . $name);
if (!$success) {
$sql="...
mysqli_query($con,$sql);
exit;
}
else
{
chmod(UPLOAD_DIR . $name, 0644);
$sql="...
mysqli_query($con,$sql);
exit;
}
}
mysqli_close($con);
Although this may not be the best way to do this it does work fine on a basic php web page. however when i put it into a jquery mobile web page it again works but doesnt seem to be posting the file im uploading. even after finding lots of articles telling me to add the data-ajax="false" to my form.
Any help would be very much appreciated.
Thanks in advance.
Turn off Ajax navigation using data-ajax="false" on your form definition because you can't upload files using Ajax.

PHP can I place a global variable inside a variable sample: $names = $_FILES ["dfile"];

Trying to build a admin panel for uploading text and images here is the html page. The Html page is working fine it's the php page that is broken the html is just here for reference.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<title>Administartor Panel</title>
<head>
<link rel="stylesheet" type="text/css" href="admin.css" />
</head>
<body>
<h1>Heritage House Administartor Panel</h1>
<br/>
<h2>
Event One
Event Two
Event Three
Event Four
Event Five
Event Six
</h2>
<br/>
<table>
<tr>
<td id="eone">
<br/>
<p>Event One</P>
<p> Please name the picture file1 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="afile" id="afile" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt1.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical One:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
<tr>
<td id="etwo">
<p >Event Two</P>
<p> Please name the picture file2 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="bfile" id="bfile" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt2.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical Two:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
<tr>
<td id="ethree" >
<p >Event Three</P>
<p> Please name the picture file3 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="cfile" id="cfile" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt3.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical Three:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
<tr>
<td id="efour" >
<p >Event Four</P>
<p> Please name the picture file4 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="dfile" id="dfile" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt4.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical Four:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
<tr>
<td id="efive" >
<p >Event Five</P>
<p> Please name the picture file5 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="efile" id="efile" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt5.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical Five:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
<tr>
<td id="esix" >
<p >Event Six</P>
<p> Please name the picture file6 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="ffile" id="file6" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt6.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical Six:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
</body>
</html>
trying to rename the files uploaded based on which form they come from on the html page afile,bfile etc.
I have tried this php file a number of different ways. I can get it to work when I eliminate the large IF statement and make 6 separate files for uploading but I was hoping to make it one file
This is what I believe makes it fail moving the global variables in to normal vars?
<?php
//This function separates the extension from the rest of the file name and returns it
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
//This if statement assigns the new file name to a variable and displays a message.
if (file_exists($_FILES["afile"]["tmp_name"]))
{
$new = "file1.";
$type = $_FILES ["afile"] ["type"];
$size = $_FILES ["afile"] ["size"];
$error = $_FILES ["afile"] ["error"];
$name = $_FILES ["afile"] ["name"];
$tmpname = $_FILES["afile"]["tmp_name"];
$names = "afile";
echo "file1 Uploaded <br />";
}
elseif (file_exists($_FILES["bfile"]["tmp_name"]))
{
$new = "file2.";
$type = $_FILES ["bfile"] ["type"];
$size = $_FILES ["bfile"] ["size"];
$error = $_FILES ["bfile"] ["error"];
$name = $_FILES ["bfile"] ["name"];
$tmpname = $_FILES["bfile"]["tmp_name"];
$names = "bfile";
echo "file2 Uploaded <br />";
}
elseif (file_exists($_FILES["cfile"]["tmp_name"]))
{
$new = "file3.";
$type = $_FILES ["cfile"] ["type"];
$size = $_FILES ["cfile"] ["size"];
$error = $_FILES ["cfile"] ["error"];
$name = $_FILES ["cfile"] ["name"];
$tmpname = $_FILES["cfile"]["tmp_name"];
$names = "cfile";
echo "file3 Uploaded <br />";
}
elseif (file_exists($_FILES["dfile"]["tmp_name"]))
{
$new = "file4.";
$type = $_FILES ["dfile"] ["type"];
$size = $_FILES ["dfile"] ["size"];
$error = $_FILES ["dfile"] ["error"];
$name = $_FILES ["dfile"] ["name"];
$tmpname = $_FILES["dfile"]["tmp_name"];
$names = "dfile";
echo "file4 Uploaded <br />";
}
elseif (file_exists($_FILES["efile"]["tmp_name"]))
{
$new = "file5.";
$type = $_FILES ["efile"] ["type"];
$size = $_FILES ["efile"] ["size"];
$error = $_FILES ["efile"] ["error"];
$name = $_FILES ["efile"] ["name"];
$tmpname = $_FILES["efile"]["tmp_name"];
$names = "efile";
echo "file5 Uploaded <br />";
}
elseif (file_exists($_FILES["ffile"]["tmp_name"]))
{
$new = "file6.";
$type = $_FILES ["ffile"] ["type"];
$size = $_FILES ["ffile"] ["size"];
$error = $_FILES ["ffile"] ["error"];
$name = $_FILES ["ffile"] ["name"];
$tmpname = $_FILES["ffile"]["tmp_name"];
$names = "ffile";
echo "file6 Uploaded <br />";
}
//This applies the function to our file
$ext = findexts ($_FILES ['$names'] ['name']) ;
//This assigns the subdirectory you want to save into.
$targett = "forms/upload/";
//This combines the directory, the new file name, and the extension
$target = $targett . $new.$ext;
// makes sure image meets specs
if ((($type == "image/gif")
|| ($type == "image/jpeg")
|| ($type == "image/pjpeg"))
&& ($size < 2000000))
{
if ( $error > 0)
{
echo "Return Code: " . $error . "<br />";
}
else
{
echo "Thank You! <br />";
}
//saves uploaded file
if (file_exists("forms/upload/" . $name))
{
move_uploaded_file( $tmpname, $target);
echo $name . " Old File Over Written. ";
}
else
{
move_uploaded_file( $tmpname, $target);
echo "Stored in: " . "forms/upload/" . $name;
}
?>
Maybe is my elseif statement broken?
I have only been playing with this php stuff for about a week so if I'm way off mark here sorry for wasting your time.
Maybe if I try making 6 functions with the globals move_uploaded_file($_FILES["file"]["tmp_name"], $target); than placeing them in the elseif statement it could work?
this is a copy of the working php page
<?php
//This function separates the extension from the rest of the file name and returns it
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
//This applies the function to our file
$ext = findexts ($_FILES['afile']['name']) ;
//This line assigns a random number to a variable. You could also use a timestamp here if you prefer.
$new = "file1.";
//This assigns the subdirectory you want to save into... make sure it exists!
$targett = "forms/upload/";
//This combines the directory, the random file name, and the extension
$target = $targett . $new.$ext;
if ((($_FILES["afile"]["type"] == "image/gif")
|| ($_FILES["afile"]["type"] == "image/jpeg")
|| ($_FILES["afile"]["type"] == "image/pjpeg"))
&& ($_FILES["afile"]["size"] < 2000000))
{
if ($_FILES["afile"]["error"] > 0)
{
echo "Return Code: " . $_FILES["afile"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["afile"]["name"] . "<br />";
echo "Type: " . $_FILES["afile"]["type"] . "<br />";
echo "Size: " . ($_FILES["afile"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["afile"]["tmp_name"] . "<br />";
if (file_exists("forms/upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["afile"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["afile"]["tmp_name"], $target);
echo "Stored in: " . "forms/upload/" . $_FILES["afile"]["name"];
}
else
{
echo "Invalid file";
}
That is a heck of a lot of repeated code! Do you really need 6 separate HTML forms? Why not have one form, with a dropdown for choosing the event?
<select name="event">
<option value="afile">Event One</option>
<option value="bfile">Event Two</option>
...etc
</select>
For the input, use:
<input type="file" name="userfile" id="file" />
Then instead of your lengthy if/else clauses, simply do something like this:
if ( isset($_POST['event']) && file_exists($_FILES['userfile']['tmp_name']) )
{
// base this code on the value of $_POST['event']
}
Maybe you should use
isset($_FILES["afile"])
instead of
file_exists($_FILES["afile"]["tmp_name"])
--edit
to answer your question in the title: it is possible, if, e.g $_FILES["afile"], is set. That way you'll have an array in your new variable.
Very close to working actually.
//Uses $names as a key in $_FILES - no single quotes for variable expansion....
$ext = findexts($_FILES[$names]['name']);
Might I also suggest using some sort of loop instead of a big elseif with a bunch of pasted code. Two improvements used: foreach on the $_FILES array, and extract
foreach ($_FILES as $fileKey => $values)
{
// Sets $type, $size, $name, $tmp_name, $error
extract($values);
//This applies the function to our file
$ext = findexts($name) ;
//This assigns the subdirectory you want to save into.
$targett = "forms/upload/";
//This combines the directory, the new file name, and the extension
$target = $targett . $new.$ext;
// makes sure image meets specs
if ((($type == "image/gif")
|| ($type == "image/jpeg")
|| ($type == "image/pjpeg"))
&& ($size < 2000000))
{
if ($error > 0)
{
echo "Return Code: " . $error . "<br />";
}
else
{
echo "Thank You! <br />";
}
//saves uploaded file
if (file_exists("forms/upload/" . $name))
{
move_uploaded_file( $tmp_name, $target);
echo $name . " Old File Over Written. ";
}
else
{
move_uploaded_file( $tmp_name, $target);
echo "Stored in: " . "forms/upload/" . $name;
}
}
}
I just wanted to thank everyone who left answers to my questions I was quite amazed at the short response time to my question. After trying and testing your answers I was still coming up short of any type of real solution to my problem it wasn't till I discovered that I forgot to end a if statement that I started making progress towards a answer.
if ((($type == "image/gif")
|| ($type == "image/jpeg")
|| ($type == "image/pjpeg"))
&& ($size < 2000000))
{
Now that this if statement has a ending and I have had time to study all of your answers I'm sure I will have this page working to my liking in no time.
It's working now but their are still refinements to be made. Here is my working html and php pages.
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<!--
html admin page for HH
-->
<title>Administartor Panel</title>
<head>
<link rel="stylesheet" type="text/css" href="admin.css" />
</head>
<body>
<h1>Heritage House Administartor Panel</h1>
<br/>
<table>
<tr>
<td id="eone">
<br/>
<p>Event Uploader</P>
<form action="formupload.php" method="post"
enctype="multipart/form-data">
<label for="selection">Please select the event you would like to edit.</label>
<select name="selection">
<option value="file1">Event1</option>
<option value="file2">Event2</option>
<option value="file3">Event3</option>
<option value="file4">Event4</option>
<option value="file5">Event5</option>
<option value="file6">Event6</option>
</select>
<br />
<p> Please upload your event picture file here.</p>
<p align="center">
<label for="file">Photo:</label>
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Upload" />
</p>
</form>
<form action="WriteTxt1.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for the event artical:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
</form>
</td>
</tr>
</body>
</html>
PHP
//This function separates the extension from the rest of the file name and returns it
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
//This if statement assigns the new file name to a variable from the value of the options menu in the html form selection.
$new = $_POST ["selection"] . ".";
//This applies the function to our file
$ext = findexts ($_FILES ["file"] ["name"]) ;
//This assigns the subdirectory you want to save into.
$targett = "forms/upload/";
//This combines the directory, the new file name, and the extension
$target = $targett . $new.$ext;
// makes sure image meets specs
if ((($_FILES ["file"] ["type"] == "image/gif")
|| ($_FILES ["file"] ["type"] == "image/jpeg")
|| ($_FILES ["file"] ["type"] == "image/pjpeg"))
&& ($_FILES ["file"] ["size"]< 2000000))
{
echo "file check pass<br />";
}
//makes an error if file does not meet standards
if ( $_FILES ["file"] ["error"] > 0)
{
echo "Return Code: " . $_FILES ["file"] ["error"] . "<br />";
}
else
{
echo "Thank You! <br />";
}
//saves uploaded file
if (file_exists("forms/upload/" . $_FILES ["file"] ["name"]))
{
move_uploaded_file( $_FILES ["file"]["tmp_name"], $target);
echo $_FILES ["file"] ["name"] . " Old File Over Written. ";
}
else
{
move_uploaded_file( $_FILES["file"]["tmp_name"], $target);
echo "Stored in: " . "forms/upload/" . $_FILES ["file"] ["name"];
}
?>
<html>
<!--
html to return user to admin page
-->
<title>
Upload Result
</title>
<head>
<p>
<br />
Back To Admin Page
</p>
</head>
<body>
<form action="">
<a href="adminarea.html">
<input type="button" value="Back" />
</a>
</form>
</body>
</html>
Note: I have only been using notepad2 and firefox to edit and error check my code. I'm guessing that their are better ways of debugging php out their right?

Categories