使用PHP从Web下载后,MP4无法播放(MP4 is not playable after downloaded from web using PHP)
我有一个可播放的.mp4文件,我使用此代码使用PHP下载文件。
Web浏览器下载文件正常,但下载后mp4文件无法播放。
代码有问题吗?
<?php // place this code inside a php file and call it f.e. "download.php" $path = $_SERVER['DOCUMENT_ROOT'] . "/video/"; $fullPath = $path."test_video.mp4"; if ($fd = fopen ($fullPath, "r")) { $fsize = filesize($fullPath); $path_parts = pathinfo($fullPath); header("Content-type: application/octet-stream"); header("Content-Disposition: filename=\"".$path_parts["basename"]."\""); header("Content-length: $fsize"); header("Cache-control: private"); //use this to open files directly while(!feof($fd)) { $buffer = fread($fd, 2048); echo $buffer; } } fclose ($fd); exit; ?>
I have a playable .mp4 file and I used this code to download the file using PHP.
Web browser downloads file ok, but the mp4 file is not playable after downloading.
Does the code have any issue?
<?php // place this code inside a php file and call it f.e. "download.php" $path = $_SERVER['DOCUMENT_ROOT'] . "/video/"; $fullPath = $path."test_video.mp4"; if ($fd = fopen ($fullPath, "r")) { $fsize = filesize($fullPath); $path_parts = pathinfo($fullPath); header("Content-type: application/octet-stream"); header("Content-Disposition: filename=\"".$path_parts["basename"]."\""); header("Content-length: $fsize"); header("Cache-control: private"); //use this to open files directly while(!feof($fd)) { $buffer = fread($fd, 2048); echo $buffer; } } fclose ($fd); exit; ?>
原文:https://stackoverflow.com/questions/39361657
最满意答案
您可以使用Lambda表达式和方法在属性上调用setValue。
创建此方法:
public void SetPropertyValue<O, T>(O obj, Expression<Func<O, T>> property, T value) { var memberSelectorExpression = property.Body as MemberExpression; if (memberSelectorExpression != null) { var p = memberSelectorExpression.Member as PropertyInfo; if (p != null) { p.SetValue(obj, value, null); } } }
然后打电话给它
SetPropertyValue(textBox1, (t=>t.BackColor), Color.Red );
看到:
正如Servy所说,性能可能对您来说是一个问题。 您还可以使用CodeProject中描述的其他技术:
http://www.codeproject.com/Articles/584720/ExpressionplusbasedplusPropertyplusGettersplusandp
You can use Lambda Expressions and a method to call the setValue on the property.
Create this method:
public void SetPropertyValue<O, T>(O obj, Expression<Func<O, T>> property, T value) { var memberSelectorExpression = property.Body as MemberExpression; if (memberSelectorExpression != null) { var p = memberSelectorExpression.Member as PropertyInfo; if (p != null) { p.SetValue(obj, value, null); } } }
Then call it
SetPropertyValue(textBox1, (t=>t.BackColor), Color.Red );
See:
How to set property value using Expressions?
As Servy sugguests, performance may be an issue for you. You can also use a different technique described at CodeProject:
http://www.codeproject.com/Articles/584720/ExpressionplusbasedplusPropertyplusGettersplusandp
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
如果你的属性都是数字的,并且可以被均匀地视为单一类型 - 比如说decimal你不需要反射。 像这样的应该做的诀窍: protected decimal ComputeFinancialSum( DateTime? beginDate, DateTime? endDate, Func
propertyToSum ) { if (beginDate == null && endD ... -
如果将其名称作为参数传递,如何在方法中打开表单(How do I open a form in a method if passed its name as a parameter)[2023-04-12]
可以使用“ 工厂方法 ”来执行此操作。 您可以像这样定义FormToOpen createForm()为了清楚起见,我将它重命名为createForm() ): Func -
您可以使用Lambda表达式和方法在属性上调用setValue。 创建此方法: public void SetPropertyValue
(O obj, Expression > property, T value) { var memberSelectorExpression = property.Body as MemberExpression; if (memberSelectorExpression != null) { var ... -
您可以像这样更改Encrypt功能: public static void Encrypt
(Action prop, T value, string username, string password) { // awesome stuff before prop(value); // awesome stuff after } 然后调用Encrypt : Encrypt(value => obj.Prop = value, 23, "", ""); Youd could cha ... -
如何创建一个可以执行传递给它的任何其他方法的方法(How to make a method that can execute any other method passed to it)[2023-08-20]
你的想法可以实现,但是做某事的功能必须始终具有相同的签名; 为此,您可以使用预定义的委托类型。 请考虑以下代码段。 public void SomethingExecuter(IEnumerableFileNames, Action Something) { foreach (string FileName in FileNames) { Something(FileName); } } public void SomethingO ... -
不,没有这方面的快捷语法 - 显式实现至少一个属性的get / set很好。 通过多个属性更改相同值的能力让人感到困惑。 确保你真的需要这样做 - 也就是说,如果你正在评论你实现的接口,你可以使其中一个接口只读。 或者,考虑至少一个接口的显式实现,以最小化同一属性的两个setter的可见性。 如果将属性设置为只读,则可以将C#6语法用于仅使用较短的get属性: public int Bar => Baz; No, there is no shortcut syntax for this - expli ...
-
我认为你要做的是使用它应该创建的对象类型的ID来参数化控制器的Create方法。 虽然ASP.NET MVC中没有关于参数名称的硬性和快速约定,但我认为大多数人都希望名为id的参数引用正在处理的对象,而不是它的属性。 考虑到这一点,我给你的建议是做以下事情: 将Create方法的GET版本中的参数重命名为类似typeId或类似的东西,因此签名看起来像public ActionResult Create(string typeId = null) 添加一个自定义路由到MvcApplication ,它将让您拥 ...
-
Java是按值传递的 。 如果将PhysicsVector对象传递给updatePosition方法,则传递的引用副本实际上指向堆上的同一对象。 您应该将对象的副本传递给updatePosition或在该方法中创建副本。 要创建副本,您可以将一个复制构造函数引入PhysicsVector类 public PhysicsVector(PhysicsVector originalVector){ // Setup the values with those contained in originalVe ...