Upload image php - php

I'm trying to upload an image in a specific folder, but I'm getting nothing, not even an error message, when I click on upload button I get a blank page, and that's it, What am I missing?
<?php if ((isset($_POST["enviado"])) && ($_POST["enviado"] == "form2")) {
$nome_arquivo = $_FILES['userfile']['name'];
move_uploaded_file($_FILES['userfile']['tmp_name'], "../legendofgames/documentos/games/".$nome_arquivo);
?>
<script>
opener.document.form2.strImage.value="<?php echo $nome_arquivo; ?>";
self.close();
</script>
<?php
}
else
{?>
.
<form method="post" enctype="multipart/form-data" id="form2">
<p>
<input name="userfile" type="file" />
</p>
<p>
<input type="submit" name="button" id="button" value="Upload Image" />
</p>
<input type="hidden" name="enviado" value="form2" />
</form>
<?php }?>

1.turn on error reporting in php.ini file or add this line at first:
<?php error_reporting(E_ALL); ?>
2.It seems that u have an syntax error on line 4: forget to close php code by ?>
<?php if ((isset($_POST["enviado"])) && ($_POST["enviado"] == "form2")) {
$nome_arquivo = $_FILES['userfile']['name'];
move_uploaded_file($_FILES['userfile']['tmp_name'], "../legendofgames/documentos/games/".$nome_arquivo);
?>
<script>
opener.document.form2.strImage.value="<?php echo $nome_arquivo; ?>";
self.close();
</script>
<?php
}
else
{?>

Related

Small Text file File upload HTML to PHP stopped working on upgrade to php 5.4 from 5.3

I have just been forced to upgrade from php 5.3 to 5.4 The file upload mechanism has stopped working. I only need to upload one file at a time. Each file is just a simple .txt file of less than 20kBytes in size.
As far as I can see the filename is not being passed to php from the HTML form.
<FORM name="form2" method="post" enctype="multipart/form-data" action="upload.php">
<P><STRONG>Upload log file:</STRONG>
<INPUT name="call" type="hidden" value="<? echo $call; ?>">
<INPUT name="logfile" type="file">
<INPUT type="submit" name="Submit2" value="Send file">
</P>
</FORM>
$logfile = $_GET[logfile];
echo "<P>" .$logfile. " for ".$call."</P>\n";
if ($logfile == "none") {
echo "<P>No file uploaded.</P>\n";
exit;
}
echo "<P>File " .$logfile_name." uploaded, ".$logfile_size." bytes.</P>\n";
In index.html
<FORM name="form2" method="post" enctype="multipart/form-data" action="upload.php">
<P><STRONG>Upload log file:</STRONG>
<INPUT name="call" type="hidden" value="<? echo $call; ?>">
<INPUT name="logfile" type="file">
<INPUT type="submit" name="Submit2" value="Send file">
</P>
</FORM>
in upload.php
<?php
$logfile = $_FILES['logfile']['name'];
echo "<P>" .$logfile. "</P>\n";
if ($logfile == "") {
echo "<P>No file uploaded.</P>\n";
exit;
}
echo "<P>File " .$logfile." uploaded, ".$_FILES['logfile']['size']." bytes.</P>\n";
echo '<pre>';
print_r($_FILES);
echo '</pre>';

how can send array in html forms to php

I want to send uploaded files address from html to php with array and then moved the uploaded files to images from tmp and checked their size
<form method="post" enctype="multipart/form-data" name="form1" id="form1" action="upload.php">
<p>
<input type="file" name="Img[]" />
</p>
<p>
<input type="file" name="Img[]" />
</p>
<p>
<input type="file" name="Img[]" />
</p>
<p>
<input type="submit" name="submit" id="submit" value="Submit">
</p>
</form>
<?php
$name=rand('0123456789',5).'jpg';
$files=array($_POST['Img[]']);
echo '<pre>';
print_r($files);
if($_FILES['$files[0]']['type'] == 'image/pjpeg'){
move_uploaded_file($_FILES[$files[]]['tmp_name'],'image/'.$name);
echo "success";
}
?>
I want to upload files with form
Input file elements never store in the $_POST array. You have to loop through the $_FILES array to get the file elements
if(isset($_POST['submit']))
{
for ($i=0; $i < count($_FILES['Img']['tmp_name']) ; $i++) {
$name=rand('0123456789',5);
if($_FILES['Img']['type'][$i] == 'image/jpeg')
{
move_uploaded_file($_FILES['Img']['tmp_name'][$i],'image/'.$name . '.jpg');
}
echo "success";
}
}
In this solution you can increase the number of input files with the same name as Img
Try this
<?php
if(isset($_POST['submit']))
{
foreach($_FILES['Img'] as $Img) {
$name=rand('0123456789',5);
if($Img['type'] == 'image/jpeg')
{
move_uploaded_file($Img['tmp_name'],'image/'.$name . '.jpg');
}
echo "success";
}
}
?>

Simple Image Upload Issue?

Ok I am new to mysql and php array system. I need this to work with my simple file script I have made. Below is the code like I said it works find. But I can not figure out how to add the extra inputs...
if(isset($_FILES["file"])) {
if ($_FILES["file"]["error"] > 0) {
// Error
$up = 0;
} else {
$name = $_FILES["file"]["name"];
move_uploaded_file($_FILES["file"]["tmp_name"], UPLOADS."/$name");
$up = 1;
}
header('Location: upload.php?upload='.$up);
}
if(!$steam->isLoggedin()) {
echo "Sorry Guest, You need to login with steam to upload! <br /> \n \n";
} else {
// Check our return status to see if it worked or not
if(isset($_GET['upload'])) {
if($_GET['upload'] == 1) {
echo 'Uploaded succesfully';
} else {
echo 'Upload failed';
}
}
}
<form action="upload.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<h1>Upload A ScreenShot</h1>
<em>Photo Name:</em><input name="name" type="text" id="name" />
<br />
<em>Size:</em><input name="size" type="text" id="size" />
<br />
<em>Category</em>
<select id="category" name="category">
<option>ScreenShots</option>
</select>
<br />
<em>Upload</em>:<input type="file" name="file" />
<br />
<input type="submit" name="Submit" value="Submit" />
</form>
The upload part works. But what I can not do is figure out how to add Size, Description and Category to the php code. I just do not know how to
set that part up and I do not know were to place the code. If anyone could help me I would really apricate it :).
$name = isset($_POST['name']); just returns true what you want to do with this line:
$filename = $name."_"."$type"."_".$rand."_".$_FILES['file']['name'];
and don't forget about enctype= multipart/form-data in html whithout it you could not save your file

Multiple file upload herror

I found a nice tutorial on YouTube by Anthoniraj Amalanathan. On the video tutorial, it works fine for hem but when I try to replicate it, I get an error. Here is the code:
<form action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post">
<input type="file" name="upload[]">
<input type="file" name="upload[]">
<input type="submit" name="send" value="Send Now">
</form>
<?php
if(isset($_FILES['upload'])=== true)
{
$files = $_FILES['upload'];
for($x=0;$x<count($files['name']);$x++)
{
$name=$files['name'][$x];
$tmp_name = $file['tmp_name'][$x];
move_uploaded_file($files,'test/'.$name);
echo 'Upload OK';
}
}
?>
The message states that the error is on line 12 ($tmp_name = $file['tmp_name'][$x];) but I don't seem to figure out why.
Can some one help here?
Try this, I tested it and it works for me.
<form action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post">
<input type="file" name="upload[]">
<input type="file" name="upload[]">
<input type="submit" name="send" value="Send Now">
</form>
<?php
if(isset($_FILES['upload'])=== true) {
$files = $_FILES['upload'];
for($x=0;$x<count($files['name']);$x++) {
$name = $files['name'][$x];
$tmp_name = $file['tmp_name'][$x];
move_uploaded_file($files['tmp_name'][$x],'test/'.$name);
echo 'Upload OK';
}
}
?>
The error I got came from using an array as the temp. file location. By changing it to $files['tmp_name'][$x], it worked.
Old: move_uploaded_file($files,'test/'.$name);
New: move_uploaded_file($files['tmp_name'][$x],'test/'.$name);
Its just a typo. $file is never declared, it should be $files.
Here:
$tmp_name = $file['tmp_name'][$x];
// ^ missing s
Also here:
move_uploaded_file($files,'test/'.$name);
// ^^^^^^ shouldn't this be $tmp_name?
Try like this :
<?php
if(is_uploaded_file($_FILES['upload']['tmp_name'])){
foreach($_FILES['upload']['name'] as $x=>$name) {
$name = basename($_FILES['upload']['name'][$x});
$folder = 'test/';
$full_path = $folder.$name ;
if(move_uploaded_file($_FILES['upload']['tmp_name'][$x], $full_path)) {
echo 'Upload OK';
} else {
echo 'Upload Failed';
}
}
}else{
echo 'Upload Not Received';
}
?>

Load php response to div on the same page after uploading files

i am uploading the files to PHP script. it process and give response. i want to load that response in the div or frame on same web page. now the following script giving response in the blank web page. please help me to solve this.
php:
<html>
<head>
<script type="text/javascript">
function init() {
if(top.uploadDone) top.uploadDone();
}
window.onload=init;
</script>
<body>
<?php
$fn = (isset($_SERVER['HTTP_X_FILENAME']) ? $_SERVER['HTTP_X_FILENAME'] : false);
if ($fn) {
file_put_contents(
'uploads/' . $fn,
file_get_contents('php://input')
);
echo "$fn uploaded";
exit();
}
else {
// form submit
$files = $_FILES['fileselect'];
//print $files['name'];
foreach ($files['error'] as $id => $err) {
if ($err == UPLOAD_ERR_OK) {
$fn = $files['name'][$id];
move_uploaded_file(
$files['tmp_name'][$id],
'uploads/' . $fn
);
if($id==1)
{
$filename = 'uploads/' . $fn;
$fp = fopen( $filename, "r" ) or die("Couldn't open $filename");
$line = fgets($fp);
fclose($fp);
$arr = split(",",$line);
for($i=2;$i< count($arr);$i++)
{
print '<input type="checkbox" name="traits" value="1">'.$arr[$i]."<br>";
}
}
}
}
}
?>
</body>
</html>
Jquery:
function init() {
document.getElementById("upload").onsubmit=function() {
document.getElementById("upload").target = "Analysis";
document.getElementById("Analysis").onload = uploadDone; }
}
function uploadDone() { //Function will be called when iframe is loaded
var ret = frames['Analysis'].document.getElementsByTagName("body")[0].innerHTML;
}
HTML script:
<div id="content_column">
<form id="upload" action="upload.php" method="POST" enctype="multipart/form-data">
<fieldset>
<div id="Geno" style="display:none">
<strong>Select Genotypic File</strong>
<input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="300000" />
<div>
<input type="file" id="fileselect" name="fileselect[]" />
</div>
</div>
<P>
<div id="Peno" style="display:none">
<strong>Select Penotypic File</strong>
<div>
<input type="file" id="fileselect1" name="fileselect[]" />
</div>
</div>
<P>
<div id="Other" style="display:none">
<strong>Select Other Files*</strong>
<div>
<input type="file" id="fileselect2" name="fileselect[]" multiple="multiple" />
</div>
</div>
<div id="submitbutton" style="display:none">
<INPUT TYPE="Submit" VALUE="Upload" />
</div>
</fieldset>
</form>
<iframe id="Analysis" name="Analysis" src="" ></iframe>
</div>
Thank you in adavance. .

Categories