PHP File Uploading - php

I'm getting index not found errors on a processing page for $_FILES. So far as I know my code is technically correct (at least the two other people who've looked at it can't find any errors either).
So first, the function that is called that displays the form with the file upload:
function portfolioEditor($p) {
echo "<form method=\"post\" action=\"" . siteurl . "/manage/update.php\">";
echo '<input type="text" name="name" id="name" class="grid4 first" value="' . $p['name'] . '" />';
echo '<input type="text" name="posttype" id="posttype" class="grid4" value="' . $p['posttype'] . '" />';
echo "\n<br />\n";
echo '<textarea name="content" id="content" class="grid8 first">' . $p['content'] . '</textarea>';
echo "\n<br />\n";
echo '<input type="hidden" name="MAX_FILE_SIZE" value="30000" />';
echo '<input name="file" value="' . $p['image'] . '" id="file" type="file" />';
echo '<input type="submit" value="Submit" name="submit" id="submit" />';
echo '<input type="hidden" value="true" id="fileup" name="fileup" />';
echo '</form>';
}
(take it as a given that the page with the form calls portfolioEditor($p) with details for $p filled in, or blanks for a new item.)
This is the update page (without the database insert yet)
$p = $_POST;
$p['url'] = str_replace(" ", "-", $p['name']);
foreach ($p as $k => $v) {
$p[$k] = addslashes($v);
//echo $v;
}
// FILE UPLOAD IF NEEDED
if(isset($p['fileup']) && $p['fileup'] == "true") {
$loc = sitepath . "/files";
$loc = $loc . basename( $_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'], $loc);
}
I have no idea why this isn't working, every resource I've seen on writing your own upload script uses almost the exact same code.

You need to add this to your form:
enctype='multipart/form-data'
So your form tag becomes:
echo "<form method=\"post\" enctype='multipart/form-data' action=\"" . siteurl . "/manage/update.php\">";

add as form tag attribute encytype="multipart/form-data"
echo "<form method=\"post\" action=\"" . siteurl . "/manage/update.php\" encytype=\"multipart/form-data\">";

When you are submitting a file, the form should have enctype="multipart/form-data" defined such as
echo "<form
method=\"post\"
enctype=\"multipart/form-data\"
action=\"" . siteurl . "/manage/update.php\">";

Related

When I search something in search box , the keyword which I entered disappears after giving the result

I have an ideal search-box which searches item from a given listing. My searching is working fine, the problem is when I enter something in search box to search, it gives me result but my search box gets empty.
For eg If I search "Electronics" in search box, it gives me result of electronics but my search box gets empty. It should be written with "Electronic" when it gives me result.
Probably, I should be using GET method instead is it so?
Here is my code for searching:
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
if (!empty($_REQUEST['term']))
{
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM category WHERE cat_name LIKE '%" . $term . "%' or parent LIKE '%" . $term . "' or cat_status LIKE '%" . $term . "'";
}
$r_query = mysql_query($sql);
if ($r_query > 1)
{
$dynamicList="";
while ($row = mysql_fetch_array($r_query))
{
// $cat_id=;
/*$dynamicList .= '
<img style="border:#666 1px solid;" src="../storeadmin/category/thumbs/' . $row['cat_id'] . '.jpg" width="77" />';*/
echo "<tr bgcolor=''>";
echo "<td>" . $row['cat_id'] . "</td>";
echo "<td><img style='border:#666 1px solid;' width='70' src='http://localhost/jaymin/My%20Store/storeadmin/category/thumbs/". $row['cat_id'].".jpg' /></td>";
//echo "<td>".$dynamicList."</td>";
echo "<td>" . $row['cat_name'] . "</td>";
echo "<td>" . $row['parent'] . "</td>";
echo "<td>" . $row['cat_status'] . "</td>";
echo "<td><a href='categoryylisting_edit.php?id=" . $row['cat_id'] . "'>Edit</a></td>";
echo "<td><a name='delete' href='categoryylisting_edit.php?id=" . $row['cat_id'] . "'>Delete</a></td><tr>";
echo "</tr>";
}
}
else {
echo "Nothing should be displayed";
}
?>
</table>
change your code to
Search: <input type="text" name="term" value="<?php echo #$_REQUEST['term']; ?>" /><br />
instead of
Search: <input type="text" name="term" /><br />
Try this,
<input type="text" name="term" value="<?php if(isset($_POST['term'])){ echo $_POST['term']; } ?>"/>
If you want this in your url use GET instead of POST like,
<form action="" method="get">
Search: <input type="text" name="term" value="<?php if(isset($_GET['term'])){ echo $_GET['term']; } ?>" /><br />
<input type="submit" value="Submit" />
</form>
This is because you are submitting the form and after form submit textbox values disappears. To overcome this try:
<input type="text" name="term" value="<?php if(isset($_POST['term'])){ echo $_POST['term']; } ?>"/>

How to get value from input with data from database

I am having a table with <input type="text" name="' . $r['0'] . '" value="' . $r['0'] . '"
populated from data that i fetch from database like this:
echo '<form id="actions" name="nonValidMainForm" method="post"><table border="2" width="100%">';
echo "<tr><td><b>Index</b></td><td><b>Email </b></td> <td><b>Date</b></td> <td><b>Name</b></td> <td><b>Surname</b></td> <td><b>Telephone Number</b></td> <td><b>Street Adress</b></td><br/>";
while($r = mysql_fetch_array($result)) {
$my[] = $r['0'];
echo '<tr>';
echo '<td>'.$roww++.'</td>';
echo '<td>
<input size="50%" type="text" name="' . $r['0'] . '" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</td>';
echo '<td>'.$r['1'].'</td>';
echo '<td>'.$r['2'].'</td>';
echo '<td>'.$r['3'].'</td>';
echo '<td>'.$r['4'].'</td>';
echo '<td>'.$r['5'].'</td>';
echo '</tr>';
}
echo "<pre>";
print_r($my);
echo "</pre>";
if(isset($_POST['unsubscribe'])){
foreach($my as $key=>$value){
$email = $value;
}
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
echo '<button style="position:fixed;bottom:5;left:5;">Change</button>';
echo '</table></form>';
The table looks like this:
I have tried this:
if(isset($_POST['unsubscribe'])){
$email = $POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
But the value is empty
So each time i press unsubscribe button the corresponding email to be deleted. How is this possible?
Your form has many elements with the same name. How can the browser determine which element's value to send to the server when the form is posted? Generally the last one takes precedence, but I suspect that behavior may be undefined and browser-specific.
If each individual table row needs to be a separately post-able form, then each row needs its own form:
echo '<td>
<form method="POST" action="somePage.php">
<input size="50%" type="text" name="email" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</form>
</td>';
That way when the browser posts the form to the server, it knows specifically which email and unsubscribe elements to use. Since there's only one of each for that form.
You have to wrap your inputs in a <form> tag.
echo '<form>';
while($r = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>'.$roww++.'</td>';
echo '<td>
<input size="50%" type="text" name="email" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</td>';
echo '<td>'.$r['1'].'</td>';
echo '<td>'.$r['2'].'</td>';
echo '<td>'.$r['3'].'</td>';
echo '<td>'.$r['4'].'</td>';
echo '<td>'.$r['5'].'</td>';
echo '</tr>';
}
echo '</form>';
if(isset($_POST['unsubscribe'])){
$email = $POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
Based on your code above it looks like it's a syntax error. Try the update below
if(isset($_POST['unsubscribe'])){
$email = $_POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' ); </script>";
}

Add a 'name' to a Paragraph tag to work with POST

I'm working on a php project where I want to receive a _POST of a paragraph in my form.
What's the best way to do that ?
I thought the easiest way to do that was adding a 'name' to the tag but that seems not to work.
<form action="Index.php" method=post enctype="multipart/form-data">
<?php
foreach($optevragenDataVanWerknemer as $info)
{
echo "<p name = 'wnr'><strong>".$info->wnr."</strong></p> </br>";
echo "<p> Afdeling : " . $info->afdeling . "</p>";
echo "<p> Functie : " . $info->ftienaam . "</p>";
echo "<p> Salaris : " . $info->salaris . "</p>";
echo "<p> Vesnaam : " . $info->vesnaam . "</p>";
echo "<p><img src='" . Config::getConfigInstantie()->getUploadMap() . '/' . $info->foto . "' alt=foto width=45 height=30></p>";
?>
<p>
<input type=hidden name="MAX_FILE_SIZE" value=1000000>
<label for=foto>Nieuwe foto </label>
<input type=file name=foto id=foto>
<input type=submit name=uploadKnop value="Upload Foto">
</p>
<?php
}
?>
$wnr = $_POST["wnr"]);
You can get only form elements (Form Elements) in the action page ie from $_POST. You can better assign the same value to a hidden element and access it.
<form action="Index.php" method=post enctype="multipart/form-data">
<?php
foreach($optevragenDataVanWerknemer as $info)
{
echo "<p><strong>".$info->wnr."</strong></p> </br>";
echo "<input type='hidden' name = 'wnr' value='{$info->wnr}'> </br>";
echo "<p> Afdeling : " . $info->afdeling . "</p>";
echo "<p> Functie : " . $info->ftienaam . "</p>";
echo "<p> Salaris : " . $info->salaris . "</p>";
echo "<p> Vesnaam : " . $info->vesnaam . "</p>";
echo "<p><img src='" . Config::getConfigInstantie()->getUploadMap() . '/' . $info->foto . "' alt=foto width=45 height=30></p>";
?>
<p>
<input type=hidden name="MAX_FILE_SIZE" value=1000000>
<label for=foto>Nieuwe foto </label>
<input type=file name=foto id=foto>
<input type=submit name=uploadKnop value="Upload Foto">
</p>
<?php
}
?>
</form>
You need to have data you want in $_POST to be within a form element. A <p> tag will never submit data. Try <textarea> or <input>
foreach($optevragenDataVanWerknemer as $info) {
echo "<textarea name = 'wnr'>" . $info->wnr . "\n";
echo "Afdeling : " . $info->afdeling . "\n";
echo "Functie : " . $info->ftienaam . "\n";
echo "Salaris : " . $info->salaris . "\n";
echo "Vesnaam : " . $info->vesnaam . "</textarea>";
}
I took out your <img> tag because that's an entirely different process to upload. If you're trying to store the raw HTML you could add it back into the <textarea> and it would submit as text.

Trying to send the value of a radio button in a form to a php variable.

I tried the following code, and I can't figure out what I'm doing wrong. I know I'm breaking rules by having the form spread out amongst two different divs, but I don't know any other way around it.
<?php
echo '<form name="form" method="POST">';
$directory = '/var/www/admin/html/content';
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
echo 'Files<br>';
while($it->valid()) {
if(!$it->isDot()) {
echo '<input type="radio" name="file_picked" value="content/' . $it->getSubPathName() . ' " id="file_picked" />' . $it->getSubPathName() . '</br>';
}
$it->next();
}
echo '<input type="submit" name="pickedName" value="Edit File" /></div>
<div class="editor">
<h1>SS Code Editor</h1>';
$file_picked = $_POST['file_picked'];
$edit_field = $_POST['edit_field'];
if(isset($_POST['pickedName'])) {
//get file contents and display in textarea box
$theData = file_get_contents($file_picked);
echo "<textarea name=\"edit_field\" id=\"edit_field\" cols=\"100\" rows=\"60\">";
echo $theData;
echo "</textarea><br />";
}
if(isset($_POST['submitChanges'])) {
//grab new textarea contents and put into file.
$theData = file_put_contents($file_picked, $edit_field);
//redraw textarea with new contents
$theData = file_get_contents($file_picked);
echo "<textarea name=\"edit_field\" id=\"edit_field\" cols=\"100\" rows=\"60\">";
echo $theData;
echo "</textarea><br />";
}
?>
<input type="submit" name="submitChanges" value="Save">
</form>
You have an extra space at the end of the checkbox input value :
Replace :
value="content/' . $it->getSubPathName() . ' " id="...
with :
value="content/' . $it->getSubPathName() . '" id="...
So file_get_contents($file_picked = $_POST['file_picked'])) don't find any file (with space at the end) and returns false, which is displayed as "" in the textarea.
Your value should be stored in $_POST['file_picked']

How to display the selected data from this form?

So this is part of my code :
echo "<form name='whatever' action='next.php' method='get'>";
while($row = mysql_fetch_assoc($qsq))
{
echo"<input type='checkbox' name='choice[]' value='" . $row['question_id'] . "' /> ". `$row['question_text'] . '<br />';`
}
echo"<br>";
echo "<input type='submit' value='submit' /></form>";
What should i do in the next.php ? I'm thinking to put the selected info in an array and display it. Then store the selected results in a table.But i am not sure with the codes.I am beginner in php can someone help me with the coding ?Thanks in advance!
First of all I cleaned up your code a little bit. (Using single quoutes around HTML trributes is uncanny).
echo ('<form name="whatever" action="next.php" method="get">');
while ($row = mysql_fetch_assoc ($qsq)) {
echo ('<input type="checkbox" name="choice[' . $row["question_id"] . ']" value="1" /> ' . $row["question_text"] . '<br />';
}
echo '<br />';
echo '<input type="submit" value="submit" /></form>';
Then you can iterate thru the values with a foreach loop in next.php.
echo ('<ul>');
foreach ($_GET["choice"] as $key => $value){
echo ('<li>' . $key . ' is ticked</li>');
}
echo ('</ul>');
Note that the array's keys hold the real information here,values wil only contain the number 1.
Take a look ath the source of resulting HTML. This is not theonly possible solution but good for learning purposes.

Categories