When I open up the web page, it just shows a blank page. Do you know why it is doing this? Could it be because of the [] in the $_SESSION? There is no javascript errors on error console and I don't php errors on screen.
Below is the javascript code where the appending occurs:
<?php
session_start();
$idx = count($_SESSION ['fileImage'] - 1);
$output = isset($_SESSION ['fileImage'][$idx]) ? $_SESSION ['fileImage'][$idx]['name'] : "";
?>
function stopImageUpload(success){
var imageNameArray = ['<?php echo $output ?>'];
var result = '';
if (success == 1){
result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';
for(var i=0;i<imageNameArray.length;i++)
{
$('.listImage').append(imageNameArray[i]+ '<br/>');
}
}
else {
result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
}
return true;
}
Below is the php script where it uploads a file which is on another page from the javascript function above:
<?php
session_start();
$result = 0;
$errors = array ();
$dirImage = "ImageFiles/";
if (isset ( $_FILES ['fileImage'] ) && $_FILES ["fileImage"] ["error"] == UPLOAD_ERR_OK) {
$fileName = $_FILES ['fileImage'] ['name'];
$fileExt = pathinfo ( $fileName, PATHINFO_EXTENSION );
$fileExt = strtolower ( $fileExt );
$fileDst = $dirImage . DIRECTORY_SEPARATOR . $fileName;
if (count ( $errors ) == 0) {
if (move_uploaded_file ( $fileTemp, $fileDst )) {
$result = 1;
}
}
}
$_SESSION ['fileImage'][] = array('name' => $_FILES ['fileImage']['name']);
?>
<script language="javascript" type="text/javascript">window.top.stopImageUpload(<?php echo $result;?>);</script>
$idx = count($_SESSION ['fileImage'] - 1);
should be
$idx = count($_SESSION ['fileImage']) -1 ;
var imageNameArray = new Array();
imageNameArray = <?php echo $output ?>;
should become
var imageNameArray = ['<?php echo $output ?>'];
Related
I'm using the following to create a list of my files in the 'html/' and link path.
When I view the array it shows, for example, my_file_name.php
How do I make it so the array only shows the filename and not the extension?
$path = array("./html/","./link/");
$path2= array("http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/html/","http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/link/");
$start="";
$Fnm = "./html.php";
$inF = fopen($Fnm,"w");
fwrite($inF,$start."\n");
$folder = opendir($path[0]);
while( $file = readdir($folder) ) {
if (($file != '.')&&($file != '..')&&($file != 'index.htm')) {
$folder2 = opendir($path[1]);
$imagename ='';
while( $file2 = readdir($folder2) ) {
if (substr($file2,0,strpos($file2,'.')) == substr($file,0,strpos($file,'.'))){
$imagename = $file2;
}
}
closedir($folder2);
$result="<li class=\"ui-state-default ui-corner-top ui-tabs-selected ui-state-active\">\n\n$file2\n<span class=\"glow\"><br></span>
</li>\n";
fwrite($inF,$result);
}
}
fwrite($inF,"");
closedir($folder);
fclose($inF);
pathinfo() is good, but I think in this case you can get away with strrpos(). I'm not sure what you're trying to do with $imagename, but I'll leave that to you. Here is what you can do with your code to compare just the base filenames:
// ...
$folder = opendir($path[0]);
while( $file = readdir($folder) ) {
if (($file != '.')&&($file != '..')&&($file != 'index.htm')) {
$folder2 = opendir($path[1]);
$imagename ='';
$fileBaseName = substr($file,0,strrpos($file,'.'));
while( $file2 = readdir($folder2) ) {
$file2BaseName = substr($file2,0,strrpos($file2,'.'));
if ($file2BaseName == $fileBaseName){
$imagename = $file2;
}
}
closedir($folder2);
$result="<li class=\"ui-state-default ui-corner-top ui-tabs-selected ui-state-active\">\n\n$file2\n<span class=\"glow\"><br></span>
</li>\n";
fwrite($inF,$result);
}
}
I hope that helps!
I want to append the name of the file which has been uploaded using the $_POST method to post the $_FILES['fileImage']['name'] from the php script. Problem is that after the file has been uploaded, I don't see the filename appended, it just shows a blank. Why is it not appending the file's name after succesful upload of the file?
If anyone could provide a coded example then it would be very helpful.
Below is Javascript code:
<?php
session_start();
$output = array();
if(isset($_POST['fileImage'])){
$idx = count($_POST['fileImage']) -1 ;
$output[] = isset($_POST['fileImage'][$idx]) ?
$_POST['fileImage'][$idx]['name'] : "";
}
?>
<script>
function stopImageUpload(success) {
var imageNameArray = <?php echo json_encode($output); ?>;
var result = '';
if (success == 1) {
result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';
for (var i = 0; i < imageNameArray.length; i++) {
$('.listImage').append(imageNameArray[i] + '<br/>');
}
}
else {
result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
}
return true;
}
</script>
Below is the php script which uploads the file and this script is on a seperate age from the Javascript function:
<?php
session_start();
$result = 0;
$errors = array ();
$dirImage = "ImageFiles/";
if (isset ( $_FILES ['fileImage'] ) &&
$_FILES ["fileImage"] ["error"] == UPLOAD_ERR_OK) {
$fileName = $_FILES ['fileImage'] ['name'];
$fileExt = pathinfo ( $fileName, PATHINFO_EXTENSION );
$fileExt = strtolower ( $fileExt );
$fileDst = $dirImage . DIRECTORY_SEPARATOR . $fileName;
if (count ( $errors ) == 0) {
if (move_uploaded_file ( $fileTemp, $fileDst )) {
$result = 1;
}
}
}
$_SESSION ['fileImage'][] = array('name' => $_FILES ['fileImage']['name']);
?>
Because you're looking in $_POST for a variable which is in $_SESSION. Try changing it to:
$output = array();
if(isset($_SESSION['fileImage'])){
$idx = count($_SESSION['fileImage']) -1 ;
$output[] = isset($_SESSION['fileImage'][$idx]) ?
$_SESSION['fileImage'][$idx]['name'] : "";
}
I am not very good with loops but I want to loop through every one of the session file values in the array. How is the foreach loop written in this situation and does it go in the php script or in the javascript code?
Below is the full php script where it uploads a file:
<?php
session_start();
$result = 0;
$errors = array ();
$dirImage = "ImageFiles/";
if (isset ( $_FILES ['fileImage'] ) && $_FILES ["fileImage"] ["error"] == UPLOAD_ERR_OK) {
$fileName = $_FILES ['fileImage'] ['name'];
$fileExt = pathinfo ( $fileName, PATHINFO_EXTENSION );
$fileExt = strtolower ( $fileExt );
$fileDst = $dirImage . DIRECTORY_SEPARATOR . $fileName;
$filePrifix = basename ( $fileName, "." . $fileExt );
$i = 0;
while ( file_exists ( $fileDst ) ) {
$i ++;
$fileDst = $dirImage . DIRECTORY_SEPARATOR . $filePrifix . "_" . $i . "." . $fileExt;
}
// Move the file
if (count ( $errors ) == 0) {
if (move_uploaded_file ( $fileTemp, $fileDst )) {
$result = 1;
}
}
}
$_SESSION ['fileImage'][] = $_FILES ['fileImage']['name'];
?>
<script language="javascript" type="text/javascript">window.top.stopImageUpload(<?php echo $result;?>);</script>
Below is the javascript function which contains the json code:
function stopImageUpload(success){
var imageNameArray = <?php echo json_encode(isset($_SESSION ['fileImage']) ? $_SESSION ['fileImage'] : null); ?>;
var result = '';
if (success == 1){
result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';
for (imagename in imageNameArray)
{
$('.listImage').append(imagename + '<br/>');
}
}
else {
result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
}
$(sourceImageForm).find('.imagef1_upload_process').css('visibility','hidden');
$(sourceImageForm).find('.imagef1_cancel').css('visibility','hidden');
$(".sbtnimage").removeAttr("disabled");
$(".sbtnvideo").removeAttr("disabled");
$(".sbtnaudio").removeAttr("disabled");
return true;
}
$_SESSION ['fileImage'][] = $_FILES ['fileImage']['name'];
Put that somewhere near the end of your PHP code.
var imageNameArray = <?php echo json_encode(isset($_SESSION ['fileImage']) ? $_SESSION ['fileImage'] : null); ?>;
Change your Javascript imagename to that.
Then make a loop:
for (imagename in imageNameArray)
{
if (success == 1)
{
result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';
$('.listImage').append(imagename + '<br/>');
}
else {
result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
}
}
I am trying to retrieve a name of the file uploaded and append it into .list. The problem is that the file name it is retrieving from the $_SESSION variable is always the last file updated and then it doesn't change.
For example if I upload these files below in the server:
desert.jpg
tulips.jpg
lighthouse.jpg
desert_2.jpg
Then the file names it should append from $_SESSION should be:
desert.jpg
tulips.jpg
lighthouse.jpg
desert_2.jpg
But instead it is appending this below for file names retrieved from $_SESSION on screen:
desert.jpg
desert.jpg
desert.jpg
desert.jpg
How can it be fixed so that when a file is uploaded onto the server, it's file name is appended onto .list?
Below is the javascript function where $_SESSION variable is in the json code which displays the name of the file on the screen by appending it onto .list:
function stopImageUpload(success){
var imagename = <?php echo json_encode(isset($_SESSION ['fileImage']['name']) ? $_SESSION ['fileImage']['name'] : null); ?>;
if (success == 1){
$('.listImage').append(imagename + '<br/>');
}
else {
$('.listImage').append('No File');
}
return true;
}
Below is the php code where it uploads the file:
<?php
session_start();
$result = 0;
$errors = array ();
$dirImage = "ImageFiles/";
$allowedTypes = array (
"image/jpeg",
"image/gif",
"image/pjpeg",
"image/jpg",
"image/png",
"image/tif"
);
if (isset ( $_FILES ['fileImage'] ) && $_FILES ["fileImage"] ["error"] == UPLOAD_ERR_OK) {
$fileName = $_FILES ['fileImage'] ['name'];
$fileSize = $_FILES ['fileImage'] ['size'];
$fileTemp = $_FILES ['fileImage'] ['tmp_name'];
$fileType = $_FILES ['fileImage'] ['type'];
$fileExt = pathinfo ( $fileName, PATHINFO_EXTENSION );
$fileExt = strtolower ( $fileExt );
if (empty ( $fileName ))
continue;
if (! in_array ( $fileType, $allowedTypes )) {
$errors [] = "File Type is not accepted";
}
if(!is_writable($dirImage ))
{
$errors [] = "File Destination not writeable";
}
$fileDst = $dirImage . DIRECTORY_SEPARATOR . $fileName;
$filePrifix = basename ( $fileName, "." . $fileExt );
$i = 0;
while ( file_exists ( $fileDst ) ) {
$i ++;
$fileDst = $dirImage . DIRECTORY_SEPARATOR . $filePrifix . "_" . $i . "." . $fileExt;
}
// Move the file
if (count ( $errors ) == 0) {
if (move_uploaded_file ( $fileTemp, $fileDst )) {
$result = 1;
}
}
}
$_SESSION ['fileImage'][] = $_FILES ['fileImage']['name'];
?>
<script language="javascript" type="text/javascript">window.top.stopImageUpload(<?php echo $result;?>);</script>
On seperate page I have a JSON which retrieves the $_SESSION:
function stopImageUpload(success){
var imagename = <?php echo json_encode(isset($_SESSION ['fileImage']) ? $_SESSION ['fileImage'] : null); ?>;
if (success == 1){
$('.listImage').append(imagename + '<br/>');
}
else {
$('.listImage').append('<br/>');
}
return true;
}
$_SESSION ['fileImage'][] = $_FILES ['fileImage']['name']; instead of $_SESSION ['fileImage']['name'] = $_FILES ['fileImage']['name'];
How do i display the die() message in if($allowed) at the same place as move_uploaded_file result?
<?php
$destination_path = $_SERVER['DOCUMENT_ROOT'].'/uploads/';
$allowed[] = 'image/gif';
$allowed[] = 'image/jpeg';
$allowed[] = 'image/pjpeg';
$allowed[] = 'image/png';
if (!in_array($_FILES['myfile']['type'], $allowed)) {
die('Wrong file type!');
}
$result = 0;
$now = time();
$ext = end(explode(".", $_FILES['myfile']["name"]));
$filename = ( $_FILES['myfile'][0].$now.".".$ext);
if(#move_uploaded_file($_FILES['myfile']['tmp_name'], $destination_path .$filename)) {
$result = 1;
}
sleep(1);
?>
<script language="javascript" type="text/javascript"> window.top.window.stopUpload('<? php echo $result; ?>', '<?php echo $filename; ?>');</script>
Javascript to display move_uploaded_file result:
function stopUpload(success,filename){
var result = '';
if (success == 1){
result = '<span class="msg">Great!<\/span><br/><br/>';
}
else {
result = '<span class="emsg">Not so great.<\/span><br/><br/>';
}
do you mean in the same condition?
if (in_array($_FILES['myfile']['type'], $allowed) && #move_uploaded_file($_FILES['myfile']['tmp_name'], $destination_path .$filename)) {
$result = 1;
} else {
die("Error while uploding file");
}
Add a third $result condition (-1, for example), then instead of die(message);, do $result = -1; exit(0);. Change the JavaScript so else if (success == -1) { result = 'Wrong file type!' }