How can I allow user to upload own PDF and have it displayed? - html

I have a website for school where every teacher is going to have a page. Each teacher's page will have a spot for them to upload a PDF. I want this to then show in a viewer on the page so students see the Viewer when they access it.
How would I code into the website allowing the user to upload a PDF and not have it replaced until somebody else uploads a PDF?
so far I have the code to upload a document.
<form method="POST" enctype="multipart/form-data" action="fup.cgi">
File to upload: <input type="file" name="upfile"><br/>
Notes about the file: <input type="text" name="note"><br/>
<br/>
<input type="submit" value="Press"> to upload the file!
</form>
How can I get it to go into a viewer below? and that it saves until replaced.

1 - First thing you are not able to upload file to server but your form action is claiming to use CGI,
2 - Second i cant really get what you want but the following code can upload files to server its in PHP and otherthing are you using SQL or what Database are you using it seems you also need database
<?php
set_time_limit(0);
if(!is_dir("uploads")){
$uploadDir = mkdir("uploads");
}
$allowedExts = array("pdf", "docx", "doc");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|| ($_FILES["file"]["type"] == "application/msword"))
&& ($_FILES["file"]["size"] < 200000000000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("/uploads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"/uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "/uploads/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
Do you want to display pdf thumb, icon, or read the pdf

Related

Make a html button open a website on the server side [duplicate]

I want to launch a bash script when a button is pressed on a website.
This is my first attempt:
<button type="button" onclick="/path/to/name.sh">Click Me!</button>
But no luck. Any suggestions?
As stated by Luke you need to use a server side language, like php.
This is a really simple php example:
<?php
if ($_GET['run']) {
# This code will run if ?run=true is set.
exec("/path/to/name.sh");
}
?>
<!-- This link will add ?run=true to your URL, myfilename.php?run=true -->
Click Me!
Save this as myfilename.php and place it on a machine with a web server with php installed. The same thing can be accomplished with asp, java, ruby, python, ...
This is really just an expansion of BBB's answer which lead to to get my experiment working.
This script will simply create a file /tmp/testfile when you click on the button that says "Open Script".
This requires 3 files.
The actual HTML Website with a button.
A php script which executes the script
A Script
The File Tree:
root#test:/var/www/html# tree testscript/
testscript/
├── index.html
├── testexec.php
└── test.sh
1. The main WebPage:
root#test:/var/www/html# cat testscript/index.html
<form action="/testscript/testexec.php">
<input type="submit" value="Open Script">
</form>
2. The PHP Page that runs the script and redirects back to the main page:
root#test:/var/www/html# cat testscript/testexec.php
<?php
shell_exec("/var/www/html/testscript/test.sh");
header('Location: http://192.168.1.222/testscript/index.html?success=true');
?>
3. The Script :
root#test:/var/www/html# cat testscript/test.sh
#!/bin/bash
touch /tmp/testfile
PHP is likely the easiest.
Just make a file script.php that contains <?php shell_exec("yourscript.sh"); ?> and send anybody who clicks the button to that destination. You can return the user to the original page with header:
<?php
shell_exec("yourscript.sh");
header('Location: http://www.website.com/page?success=true');
?>
Reference: http://php.net/manual/en/function.shell-exec.php
This is how it look like in pure bash
cat /usr/lib/cgi-bin/index.cgi
#!/bin/bash
echo Content-type: text/html
echo ""
## make POST and GET stings
## as bash variables available
if [ ! -z $CONTENT_LENGTH ] && [ "$CONTENT_LENGTH" -gt 0 ] && [ $CONTENT_TYPE != "multipart/form-data" ]; then
read -n $CONTENT_LENGTH POST_STRING <&0
eval `echo "${POST_STRING//;}"|tr '&' ';'`
fi
eval `echo "${QUERY_STRING//;}"|tr '&' ';'`
echo "<!DOCTYPE html>"
echo "<html>"
echo "<head>"
echo "</head>"
if [[ "$vote" = "a" ]];then
echo "you pressed A"
sudo /usr/local/bin/run_a.sh
elif [[ "$vote" = "b" ]];then
echo "you pressed B"
sudo /usr/local/bin/run_b.sh
fi
echo "<body>"
echo "<div id=\"content-container\">"
echo "<div id=\"content-container-center\">"
echo "<form id=\"choice\" name='form' method=\"POST\" action=\"/\">"
echo "<button id=\"a\" type=\"submit\" name=\"vote\" class=\"a\" value=\"a\">A</button>"
echo "<button id=\"b\" type=\"submit\" name=\"vote\" class=\"b\" value=\"b\">B</button>"
echo "</form>"
echo "<div id=\"tip\">"
echo "</div>"
echo "</div>"
echo "</div>"
echo "</div>"
echo "</body>"
echo "</html>"
Build with https://github.com/tinoschroeter/bash_on_steroids

How to update header.phtml with hardcoded image

I am trying to remove an image from a header.phtml file. It has been hard coded into the header.phtml in addition to the regular magento header image. I cannot access the image I want to delete through magento. I have found the line of code where it has been added to the header.phtml but don't know what the next step is to change this... can I delete the header.phtml file from our sftp and then re-upload it with the appropriate line of code deleted or will that ruin the whole site?
Your help is very much appreciated! I am a novice. HTML good.. everything else clueless but definitely know the line of code that has to go.
Thank you!
Maybe you add the echo function " ";
For example
<table border = "3">
     <tr>
         <td>
             <? php
             $ files = glob ("img / *");
             foreach ($ files as $ file) {
                 echo "<div class = 'divimages'>";
                 echo '<img src = "'. $ file. '" />';
                 echo "<input type = 'submit' value = 'Delete image' /> <br>";
                 echo "</ div>";
             }
             ?>
         </ td>
     </ tr>
</ table>

Upload form duplicate file in mysql when I refresh browser

I have a problem with refreshing upload file form. First time when I clik Upload File it uploads file on server (folder uploads/), and path name uploads in mysql base (table 'files'). When I refresh that page it duplicates same path name in mysql base, it does not duplicate file in uploads/. I found similar solutions but not my case, where I need to stay on the same page, and list all uploaded files. Here is the form code :
<?php
if(isset($_POST['upload'])){
$targetfolder = "uploads/";
$target_file = $targetfolder . basename( $_FILES['file']['name']) ;
$ok=1;
$file_name = $_FILES['file']['name'];
$file_type=$_FILES['file']['type'];
if ($file_type=="application/pdf" || $file_type=="image/gif" || $file_type=="image/jpeg" || $file_type== "application/msword" || $file_type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ) {
if ($ok == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
$sql = "INSERT INTO files (path, profesor, struka) VALUES ('" .$file_name. "', '". $_SESSION['username']. "','')";
if (mysqli_query($link, $sql)){
}else {echo "ERROR connecting database $sql".mysqli_error($link);
}
} else {
echo "Sorry, there was an error uploading your file.";
}
}}
else {
echo "Sorry only PDF, DOCX, JPEG, GIF.<br>";
}}
?>
<div class = "upload">
<form method="post" enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Select file for upload:</br>
<input type="file" name="file" >
<input type="submit" id = "button_upload" value="Upload File" name="upload">
</form>
</div>
first, i don't see a place in your code, where you actually show the list of uploaded files.
second - if you refresh your page, the form is submited one more time, so another insert is done with the same data. you don't see the duplicate file, because it is just overwritten.
so, after you successfully inserted and uploaded, you should do a redirect to the same page, for example with header('Location: yourscript.php'). in this case POST will not be set.

Photo Resize when loading... PHP photo from server

I'm wondering if anyone could provide me with some advice on where I went wrong. I'm trying to get a photo to resize when loaded in a DIV but the photo keeps taking it's own dimensions. Does anyone have any idea why my code isn't working? Thank you in advance!
if($row['coverphoto'] === NULL){
echo"<li>";
echo"<img src='images/slider-4.jpg'>";
echo "<div class='banner'>Customize Your Banner!</div>";
echo "</li>";
}
else{
echo "<li>";
echo "<img style='height:auto; width:auto; max-width:1025px; max-height:503px;' src='/coverphotos/" . $row['coverphoto'] . "'/><br />";
echo "<div class='banner'>Customize Your Banner!</div>";
echo "</li>";
}
?>
Try this,
else{
echo "<li>";
echo "<div style="width:50px;height:50px;"><img style='height:50px; width:50px;' src='/coverphotos/" . $row['coverphoto'] . "'/></div><br />";
echo "<div class='banner'>Customize Your Banner!</div>";
echo "</li>";
}
If it is not worked means, please check your, css file
<li>
styles. May that style makes this mistake.

Back button on the image preview

I have a minor problem, i just want a simple image preview with a go back button or something similar, I made
<div id="image1">
<a href="../assets/image_large1.jpg"><img src="../assets/image_1.jpg" width="160" height="107" alt="Example 1" />
</div>
but I don't know how to put a button inside
how could I do it?
In the most simple solution to your problem, I would do special show_image.php page, for example:
<?php
$directory = 'assets/';
$filename = $_GET['image'];
if(empty($filename) || is_file( $directory . $filename ))
{
echo 'No such image!';
}
else
{
echo '<img src="' . $directory . $filename . '" />';
}
echo '<br />';
echo 'Go back';
exit;
And link for the image will be: http://mysite.com/show_image.php?image=image_1.jpg
Of course this code should be made more secure and this thing could be done other ways.