File upload not posting data to iframe - php

I have a simple file upload form which posts to an iframe and the i retrieve the value from the iframe and redirect to another page.
The problem is the form isnt posting to the iframe (i dont think) so none of the php is being executed and null is being return to the javascript.
HTML Code
<form id="upload" name="upload" enctype="multipart/form-data" method="post" action="index.php" target="upload_target">
<input name="folder" type="hidden" value="<?php echo $folder ?>" />
<input name="filename" type="hidden" value="<?php echo $filename ?>" />
<input name="uploaded_file" type="file" size="5120" id="uploaded_file" accept="image/jpeg" />
<input id="sent" name="sent" type="submit" value="Upload" />
</form>
<iframe id="upload_target" name="upload_target" style="width:10px; height:10px; display:none"></iframe>
Javascript to get the value from iframe
$('form#upload').submit(function(){
$('#upload_wrapper').hide();
$('#loading').show();
// get the uploaded file name from the iframe
$('#upload_target').unbind().load( function()
{
var img = $('#upload_target').contents().find('#filename').html();
alert(img); //returning null
$('#loading').hide();
});
});
And the PHP at the top of the page to upload the file
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$targetFolder = $_POST['folder'] . "/";
$filename2 = $_POST['filename'];
if (!is_dir($targetFolder))
{
mkdir($targetFolder, 0777);
}
$tempFile = $_FILES['uploaded_file']['tmp_name'];
$targetPath = dirname(__FILE__) . '/' . $targetFolder;
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['uploaded_file']['name'];
$uploadfile = $targetFolder. basename($filename2 .".jpg");
move_uploaded_file($tempFile,$uploadfile);
$fileName = $uploadfile;
echo "<div id='filename'>$fileName</div>";
}
This code has been working before so im unsure at why it has stopped now please help.

Related

Upload Image and Display on Separate Page Using PHP

I'm stuck trying to get an uploaded image to display from an HTML form onto a PHP page.
All I want to do is have the image display on the second page.
Page One Code (HTML Form)
<form action="ShowReunionPhoto.php" method="POST" enctype="multipart/form-data">
Name: <input type="text" name="name" />
Description <input type="text" name="description" />
Upload Photo: <input type="file" name="reunionPhoto" />
<input type="submit" name="submit" value="Upload Photo" />
</form>
Page Two (PHP)
<?php
$Name = $_POST['name'];
$Description = $_POST['description'];
$Reunion = fopen("Reunion.txt", "ab"); {
fwrite($Reunion, $Name . "\n" . $Description . "\n");
fclose($Reunion);
}
$ReunionImage = $_FILES["file"]["reunionPhoto"];
echo '<img src="' . $ReunionImage . '">';
echo "<pre>\n";
echo readfile("Reunion.txt");
echo "</pre>\n";
?>
In order to make your image accessible, it needs to be copied to your project's directory.
Let's say you want to copy it to the images directory, relative to the script. Make sure your images directory exists.
$uploadedFile = $_FILES['reunionPhoto'];
$sourcePath = $uploadedFile['tmp_name'];
$destinationFileName = $uploadedFile['name'];
$destinationPath = __DIR__ . '/images/' . $destinationFileName;
move_uploaded_file($sourcePath, $destinationPath);
Then, in your <img> you can display that image.
you can even use the phpUpload library
$pUp = new phpUpload($_FILES['file']);
$pUp->maxSize('10');
$pUp->allowedType(["image/jpeg"]);
$pUp->newName("New_name");
if($pUp->run("test", true)){
echo "<img src='test/$pUp->output' />";
}

PHP Form seems to change action to a different script

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">

Upload in files in for loop

I am using this upload script http://www.dropzonejs.com
The upload php part is:
foreach($_POST["id"] as $key=>$value)
{
$id = trim(mysqli_real_escape_string($mysqli, $value));
$pre_set = trim(mysqli_real_escape_string($mysqli, $_POST['pre_set'][$key]));
$keep_filename = trim(mysqli_real_escape_string($mysqli, $_POST['keep_filename'][$key]));
$ds = DIRECTORY_SEPARATOR;
$storeFolder = '../uploads';
if (!empty($_FILES))
{
$tempFile = $_FILES['file']['tmp_name'];
$targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;
if($keep_filename == 'yes')
{
$targetFile = $targetPath. $pre_set.'_'.$id.'_'.$_FILES['file']['name'];
}
else
{
$targetFile = $targetPath. $pre_set.'_'.$id.'.'.pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
}
move_uploaded_file($tempFile,$targetFile);
}
}
When using this with an standard form as shown below the file is correctly uploaded and renamed:
<form action="includes/upload.php" class="dropzone">
<input type="hidden" class="form-control" id="id[]" name="id[]" value="'.$row['id'].'">
<input type="hidden" class="form-control" id="pre_set[]" name="pre_set[]" value="logo">
<input type="hidden" class="form-control" id="keep_filename[]" name="keep_filename[]" value="no">
</form>
But when using a for loop situation an image is shown in the upload window but the file is not uploaded.
I am not getting any errors (or not using the correct parameter to see it)
while($row = mysqli_fetch_array($res))
{ $i++;
echo'
<div class="container">
<form action="includes/upload.php">
<div class="form-group col-md-3">';
if($i == 1) { echo '<label>Voeg foto toe</label>'; } echo'
<div class="dropzone dropzone_small" id="myId'.$i.'">
<div class="fallback">
<input type="hidden" class="form-control" id="id[]" name="id[]" value="xxxx">
<input type="hidden" class="form-control" id="pre_set[]" name="pre_set[]" value="toolbox_">
<input type="hidden" class="form-control" id="keep_filename[]" name="keep_filename[]" value="yes">
</div>
</div>
</div>
</form>';
}
Any suggestions to change the code to get this working?
Help is much appreciated.
You must start your form with
<form action="includes/upload.php"> method="post" enctype="multipart/form-data">
// your form attributes here
</form>

Send file to server derictory over form with user's name

I have a form.html
<form action="user.php" method="post" enctype="multipart/form-data" >
<input type="text" size="40" name="UserCallFile" >
<input type="file" name="filename"><br>
<input type="submit" value="Send"><br>
</form>
And user.php
<?php
if(is_uploaded_file($_FILES["filename"]["tmp_name"]))
{
move_uploaded_file($_FILES["filename"]["tmp_name"], "img/" . $_FILES["filename"]["name"]);
} else {
echo("Error");
}
?>
I need that user could download file to server which name of this file he written on text input. Ho to do that?
I tried this way
move_uploaded_file($_FILES["filename"]["tmp_name"], "img/" . $_POST["fileName"] . "." . "png");
But of course here "png" have to be logic with any file extension.
I hope you are understand the idea of problem.
Simply try to play with this..
$image = $_FILES["filename"]["tmp_name"];
$dir = "gallery/"; //adjust this correctly, watch for slashes depending on your website config
$newname = $image['name'];
if(!#copy($image, $dir . $newname))
{
echo 'error';
}

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 . '" />';

Categories