HTML code to upload images - html

Can someone please provide me with some links (or source code) for me to research about how to upload images to a website (by the community) and for these images to be viewed online by the community.
I have searched via google, but have had no luck.
Thank you.
EDIT
Sorry, I have actually seen many examples to upload files. I am wanting (if possible) to be able to upload images, and then have them arranged in some kind of gallery so that the community can see/view them.
I am after a range of different technologies that are available on the WWW if possible.

Copy the below codes and save it as upload.php or anything.php (note it should be saved as php extension and should be run on apache server or any server supporting php).
<?php
if(isset($_REQUEST['submit']))
{
$filename= $_FILES["imgfile"]["name"];
if ((($_FILES["imgfile"]["type"] == "image/gif")|| ($_FILES["imgfile"]["type"] == "image/jpeg") || ($_FILES["imgfile"]["type"] == "image/png") || ($_FILES["imgfile"]["type"] == "image/pjpeg")) && ($_FILES["imgfile"]["size"] < 200000))
{
if(file_exists($_FILES["imgfile"]["name"]))
{
echo "File name exists.";
}
else
{
move_uploaded_file($_FILES["imgfile"]["tmp_name"],"uploads/$filename");
echo "Upload Successful . <a href='uploads/$filename'>Click here</a> to view the uploaded image";
}
}
else
{
echo "invalid file.";
}
}
else
{
?>
<form method="post" enctype="multipart/form-data">
File name:<input type="file" name="imgfile"><br>
<input type="submit" name="submit" value="upload">
</form>
<?php
}
?>
And you should create two folders("uploads" and "tmp") in the parent directory (Where the script executes).
Now your upload script is ready. It's simply an upload script with which you can upload one image at a time.

Here's a good, basic start using PHP: http://www.w3schools.com/php/php_file_upload.asp

In order to get a list of files in a directory (assumes the file path is known ahead of time) you can do something like this:
$filePath = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'uploads';
$files = scandir($filePath);
If you wanted to filter this down to look for image files you could use something like this:
$filePath = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'uploads';
$files = scandir($filePath);
$imageFiles = array_values(preg_grep('/^.*?\.((gif)|(png)|(jpe?g))$/', $files));
And for a good guide on uploading the files you can check out http://www.exchangecore.com/blog/how-upload-files-html-php/

Related

trying to extract data from txt file into and html

guys I'm doing a project game, and I'm having a struggle when I try to extract a text file content into an HTML I made.
currently, the game creates a score.txt file with the player score, and I'm trying to make an HTML that will show this score in the HTML automatically.
I will add my current HTML for reference so you could check it out:
This is the part of the code I'm struggling with
<h1><div id ="score">The score is <?p$ filename =
fopen('score.txt', 'r');
$file = fread($filename, filesize('score.txt'));
echo $file;
fclose($filename); </div> </h1>
<hr class="w3-border-grey" style="margin:auto;width:40%">
<p class="w3-large w3-center">Good job traveler!</p>
</div>
<div class="w3-display-bottomleft w3-padding-large">
<?php $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!"); echo fread($myfile,filesize("webdictionary.txt")); fclose($myfile); ?>
This should solve it
If in any case it doesn't work please refer to this url https://www.w3schools.com/php/php_file_open.asp

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.

Upload and retrieve image in Laravel

I need to save the image for an avatar.
Can anyone give me a simple code to save and retrieve image?
I need to:
Save image in folder
Save image name in DB
Finally retrieve on image tag; I have to do it by Query Builder
Form:
<form action="" method="post" role="form" multiple>
{{csrf_field()}}
<legend>Form Title</legend>
<div class="form-group">
<label for="">Your Image</label>
<input type="file" name="avatar">
</div>
<button type="submit" class="btn btn-primary">save</button>
back
</form>
<img name="youravatar" src="">
</div>
Route:
Route::get('pic','avatarController#picshow');
Route::post('pic','avatarController#pic');
Controller:
I have the avatarController, but it is empty because I don't know what to do.
Database:
Table name: avatar
Fields: name id, imgsrc, created_at, Updated_at
Other:
I found this code but I can't find out anything:
if ($request->hasFile('avatar')) {
$file = array('avatar' => Input::file('avatar'));
$destinationPath = '/'; // upload path
$extension = Input::file('avatar')->getClientOriginalExtension();
$fileName = rand(11111,99999).'.'.$extension; // renaming image
Input::file('avatar')->move($destinationPath, $fileName);
}
First, make sure you have the encrypt attribute in your form
<form action="#" method="post" enctype="multipart/form-data">
You can use something similar to this in your controller
public function save(Request $request)
{
$file = $request->file('file');
// rename your file
$name = $file->getClientOriginalName();
\Storage::disk('local')->put($name, \File::get($file));
return "file saved";
}
Yes, you should store the file route in your database as well.
Make sure you are using a consistent path for your images like
Finally you have to create a route to give public access to your image file, like so:
Route::get('images/{file}', function ($file) {
$public_path = public_path();
$url = $public_path . '/storage/' . $file;
// file exists ?
if (Storage::exists($archivo))
{
return response()->file($pathToFile);
}
//not found ?
abort(404);
});
Check the docs about Laravel Responses
I hope this gives you an Idea of what to do.
Upload Image in laravel 5.4
check if request has image
$request->hasFile('image')
OR
$request->file('image')->isValid()
Now Save Image
$request->inputname->store('folder-name') return image path 'folder name/created image name
$request->image->store('images')
Check if image exits
Storage::disk('local')->exists('image name');
Delete Image
Storage::delete('image');
This is my code
if ($request->hasFile('image') && $request->file('image')->isValid())
{
$path = $request->image->store('images');
if(!empty($path)){
$edit = Model::FindOrFail($id);
// Delete old image
$exists = Storage::disk('local')->exists($edit->image);
if($exists){
Storage::delete($edit->image);
}
$edit->image = $path;
$edit->save();
}
}
Reference

Why won't my comment system work? [HTML]

I am a beginner in html, and I am beginning to develop my first website. I watched a video to learn to code a commentary system, here is the link:
https://www.youtube.com/watch?v=O4BkHj7Ws9U
My page does resemble what is shown in the video, but there is some errors, take a look at my webpage after watching the video, as you can see, there are errors:
Here is my code:
<HTML>
<form action="" method="post">
<label> Name: <br><input type="text" name="name"><br></label>
<label> Message: <br><textarea cols="35" rows="5" name="mes"></textarea></label><br>
<input type="submit" name="post" value="Post">
</form>
</HTML>
<?php
$name = $_POST["name"];
$text = $_POST["mes"];
$post = $_POST["post"];
if ($post){
#WRITE DOWN COMMENTS#
$write = fopen("com.txt", "a+");
fwrite($write, "<u><b> $name</b></u><br>$text<br></br>");
fclose($write);
#DISPLAY COMMENTS#
$read = fopen("com.txt", "r+t");
echo "All comments:";
while(!feof($read)){
echo fread($read, 1024);
}
fclose($read);
}
else{
#DISPLAY COMMENTS#
$read = fopen("com.txt", "r+t");
echo "All comments:<br>";
while(!feof($read)){
echo fread($read, 1024);
}
fclose($read);
}
?>
Thank you for your help, sorry, I'm sort of a newbie at this.
You should use something like MAMP to run php code.
Or upload everything to a webserver.
You have to save your file as a PHP file.

HTML form field data in a text file

I want to retrieve the HTML form field data in a text file on my desktop.
I am having a HTML page containing the HTML form field box.
Further, I want to run a bash script taking the text file as a input.The bash script contains the command :
sed -f replacer input.txt > output.txt
I have to also include this output.txt file in another html form field.
Help me out.
Thank You.
Have html like this:
<form id="some" name="someName" method="post" action="/ur/url/to/post">
<input type="text" id="some1" class="someClass" value="" name="fileWrite"/>
<iput type="submit" value="submit" class="submitClass"/>
</form>
Have a php script like this:
$myFile = "testFile.txt";
if(isset($_POST['fileWrite']) && !empty($_POST['fileWrite'])) {
$fileWrite = $_POST['fileWrite'];
}
if($fileWrite) {
$fh = fopen($myFile, 'a') or die("can't open file"); //Make sure you have permission
fwrite($fh, $fileWrite);
fclose($fh);
exec('/your/command /dev/null 2>/dev/null &');
}
Thats it. Job is done.