I need some help with PHP Class. I have limited knowledge about how it works, and am trying to discover more about it. In the mean time I have an issue where I need to grab the variable after it runs through these functions within a class.
protected function upcount_name_callback($matches) {
$index = isset($matches[1]) ? intval($matches[1]) + 1 : 1;
$ext = isset($matches[2]) ? $matches[2] : '';
return ' ('.$index.')'.$ext;
}
protected function upcount_name($name) {
return preg_replace_callback(
'/(?:(?: \(([\d]+)\))?(\.[^.]+))?$/',
array($this, 'upcount_name_callback'),
$name,
1
);
}
I need to retrieve this variable in the following JS statement and send to my INSERT php file.
$('#albumBack.fileupload').bind('fileuploaddone',function(e,data) {
//Loop through each page and return object and write to DB
$.each(data.files, function (index, file) {
var filename = file.name;
$.ajax({
type: "POST",
url: "../albumUploader/queries/albumPages.php",
data: {file: filename}
});
});
});
The filename that I am getting currently is the original name, not the appended name.
Thanks for any help on this.
albumPages.php
//Variables for gallerimage table
$originalName = $_POST['file'];
$clientRef = $_SESSION['clientRef'];
$galleryID = $_SESSION['newGalleryId'];
$galleryLayout = $_SESSION['layoutID'];
$imageID = rand();
//Find the sort# for the gallery
$qSortOrder = mysql_query("SELECT MAX(sort) AS sortOrder
,id
,clientRef
,galleryId
FROM galleryimage
WHERE galleryId='{$galleryID}'
AND clientRef= '{$clientRef}'
");
$fsortOrder = mysql_fetch_array($qSortOrder);
//Latest revision
$orderNumber = $fsortOrder['sortOrder'];
$order = $orderNumber + 1;
$query = "INSERT INTO galleryimage
(
galleryId
,image
,OrgImageName
,clientRef
,sort
,layout
) VALUES (
'{$galleryID}'
,'{$imageID}'
,'{$originalName}'
,'{$clientRef}'
,'{$order}'
,'{$galleryLayout}'
)";
$return = mysql_query($query);
$originalName is the $variable that I need to define as img.jpg, $img(1).jpg etc. I am just POSTING file, which ends up being the original selected file from the input.
This is the form that selects the file(s).
<form class="fileupload" id="albumBack" action="../js/jQuery-file-upload/server/php/" method="POST" enctype="multipart/form-data" >
<!-- The fileupload-buttonbar contains buttons to add/delete files and start/cancel the upload -->
<div class="row fileupload-buttonbar">
<span class="btn btn-success fileinput-button">
<span>Choose Back</span>
<input type="file" name="files[]">
</span>
</div>
Without knowing the contents of albumBack.php, it’s hard to ascertain exactly the behavior you’ll see. But lets assume that albumBack.php calls
upcount_name($name)at some point.What happens then is that preg_replace_callback is called. This function specifically takes a string and a regex expression, along with an additional ‘callback’ function. It runs the regex on the string and gets a set of matches which is then passed to the callback function. Callbacks allow you to ‘inject’ behavior in a flexible manner: in this case, the callback is a way of defining what the replacement string should be for each regex match.
The
upcount_name_callbackfunction is what is being called here. To understand what it does you have to understand the regex expression. This is a bit complicated, but essentially it’s looking for some number (the[\d]+part), a.followed by an end of line.upcount_name_callbacktakes this as a number, which it increments and an extension, which it returns as-is if it exists, and as an empty string if not.The upshot is that this appears to change ‘blah.jpg’ to ‘blah1.jpg’ and ‘blah1.jpg’ to ‘blah2.jpg’. However, it looks like if the name you’re getting back is the same, it’s not actually being executed. You would need to post the calling lines of code from albumBack.php to confirm this.